Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ dotenv.config({

export type ConfigType = {
APP_TOKEN_EXP: string;
PORT: string;
APP_TOKEN_KEY: string;
PORT: string;
CS_API: {
US: string;
EU: string;
Expand Down
26 changes: 20 additions & 6 deletions src/middlewares/auth.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
// middleware/authentication.middleware.ts
import { Request, Response, NextFunction } from "express";
import jwt from "jsonwebtoken";
import { config } from "../config";

export const authenticateUser = (
req: Request,
res: Response,
next: NextFunction
): void => {
// Simulate authentication logic (e.g., check for a token)
const isAuthenticated = /* Your authentication logic here */ true;
if (isAuthenticated) {
next(); // User is authenticated, proceed to the next middleware or route handler
) => {
// authentication logic (check for a valid token)
const token = req.headers.authorization?.split(" ")[1];
const secretKey = config.APP_TOKEN_KEY;
if (token) {
jwt.verify(token, secretKey, (err, decoded) => {
if (err) {
return res
.status(401)
.json({ message: "Unauthorized - Invalid token" });
}

// Attach the decoded token to the request object for later use
(req as any).decodedToken = decoded;

next();
});
} else {
res.status(401).json({ message: "Authentication failed" });
return res.status(401).json({ message: "Unauthorized - Token missing" });
}
};
5 changes: 5 additions & 0 deletions src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export interface UserProfile {
};
}

export interface MigrationPayload {
region: string;
user_id: string;
}

export interface LoginServiceType {
data: any;
status: number;
Expand Down
22 changes: 12 additions & 10 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Request } from "express";
import { config } from "../config";
import jwt from "jsonwebtoken";
import { safePromise } from "../utils/index";
import https from "../utils/https.utils";
import * as fs from "fs/promises";
import { LoginServiceType, UserProfile } from "../models/types";
import {
LoginServiceType,
MigrationPayload,
UserProfile,
} from "../models/types";
import { constants } from "../constants";
import { generateToken } from "../utils/jwt.utils";

const login = async (req: Request): Promise<LoginServiceType> => {
//TODO: 1. request validation, 2. saving the authtoken in DB
Expand Down Expand Up @@ -41,14 +45,12 @@ const login = async (req: Request): Promise<LoginServiceType> => {
status: constants.HTTP_CODES.BAD_REQUEST,
};

const app_token = jwt.sign(
{
region: userData?.region,
user_id: res?.user.uid,
},
config.APP_TOKEN_KEY,
{ expiresIn: config.APP_TOKEN_EXP }
);
const migration_payload: MigrationPayload = {
region: userData?.region,
user_id: res?.user.uid,
};
// JWT token generation
const app_token = generateToken(migration_payload);

const response = {
data: {
Expand Down
10 changes: 10 additions & 0 deletions src/utils/jwt.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// src/utils/jwt.utils.ts
import jwt from "jsonwebtoken";
import { MigrationPayload } from "../models/types";

const secretKey = process.env.JWT_SECRET_KEY ?? "default_secret_key";

// @typescript-eslint/no-explicit-any
export const generateToken = (payload: MigrationPayload): string => {
return jwt.sign(payload, secretKey, { expiresIn: "5d" });
};