From a744b8c9b9649837411cde7d7f29081f37c50f02 Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 13:42:12 +0530 Subject: [PATCH 1/7] chore: saving auth-related info in the DB. --- src/controllers/auth.controller.ts | 8 ++++---- src/services/auth.service.ts | 29 +++++++++++------------------ 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index f1e56f2a..c378433f 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -1,18 +1,18 @@ import { Request, Response } from "express"; -import { userService } from "../services/auth.service"; +import { authService } from "../services/auth.service"; const login = async (req: Request, res: Response) => { - const resp = await userService.login(req); + const resp = await authService.login(req); res.status(resp?.status).json(resp?.data); }; const RequestSms = async (req: Request, res: Response) => { - const resp = await userService.requestSms(req); + const resp = await authService.requestSms(req); res.status(resp.status).json(resp.data); }; const getUserProfile = async (req: Request, res: Response) => { - const user = await userService.getUserProfile(req); + const user = await authService.getUserProfile(req); res.status(200).json(user); }; diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 2418c675..02a78428 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -14,9 +14,10 @@ import { BadRequestError, InternalServerError, } from "../utils/custom-errors.utils"; +import AuthenticationModel from "../models/authentication"; const login = async (req: Request): Promise => { - //TODO: 1. request validation, 2. saving the authtoken in DB + //TODO: 1. request validation const userData = req?.body; try { @@ -58,31 +59,23 @@ const login = async (req: Request): Promise => { region: userData?.region, user_id: res?.data?.user.uid, }; + + // Saving auth info in the DB + await AuthenticationModel.create({ + ...migration_payload, + authtoken: res?.data.user?.authtoken, + }); + // JWT token generation const app_token = generateToken(migration_payload); - const response = { + return { data: { message: constants.HTTP_TEXTS.SUCCESS_LOGIN, app_token, }, status: constants.HTTP_CODES.OK, }; - - // Write the data to a JSON file (e.g., tokens.json) - //TODO: remove this temp localStorage file, and use DB instead - await fs.writeFile( - "tokens.json", - JSON.stringify( - { - [app_token]: res?.data.user?.authtoken, - }, - null, - 2 - ) - ); - - return response; } catch (err) { throw new InternalServerError(); } @@ -163,7 +156,7 @@ const getUserProfile = async ( } }; -export const userService = { +export const authService = { login, requestSms, getUserProfile, From 2d25e0a843a76c4d00d538fd183e9f26f63fc28c Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 15:39:16 +0530 Subject: [PATCH 2/7] chore: bifurcated the authRoute into a new route: userRoute. --- src/controllers/auth.controller.ts | 6 ---- src/controllers/user.controller.ts | 11 ++++++ src/routes/auth.routes.ts | 11 ------ src/routes/user.routes.ts | 10 ++++++ src/server.ts | 2 ++ src/services/auth.service.ts | 48 +------------------------- src/services/user.service.ts | 54 ++++++++++++++++++++++++++++++ 7 files changed, 78 insertions(+), 64 deletions(-) create mode 100644 src/controllers/user.controller.ts create mode 100644 src/routes/user.routes.ts create mode 100644 src/services/user.service.ts diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index c378433f..4537961f 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -11,13 +11,7 @@ const RequestSms = async (req: Request, res: Response) => { res.status(resp.status).json(resp.data); }; -const getUserProfile = async (req: Request, res: Response) => { - const user = await authService.getUserProfile(req); - res.status(200).json(user); -}; - export const authController = { login, RequestSms, - getUserProfile, }; diff --git a/src/controllers/user.controller.ts b/src/controllers/user.controller.ts new file mode 100644 index 00000000..dbf58172 --- /dev/null +++ b/src/controllers/user.controller.ts @@ -0,0 +1,11 @@ +import { Request, Response } from "express"; +import { userService } from "../services/user.service"; + +const getUserProfile = async (req: Request, res: Response) => { + const user = await userService.getUserProfile(req); + res.status(200).json(user); +}; + +export const userController = { + getUserProfile, +}; diff --git a/src/routes/auth.routes.ts b/src/routes/auth.routes.ts index 286f440e..3532a251 100644 --- a/src/routes/auth.routes.ts +++ b/src/routes/auth.routes.ts @@ -1,6 +1,5 @@ import express from "express"; import { authController } from "../controllers/auth.controller"; -import { authenticateUser } from "../middlewares/auth.middleware"; import { asyncRouter } from "../utils/async-router.utils"; const router = express.Router(); @@ -11,14 +10,4 @@ router.post("/user-session", asyncRouter(authController.login)); // SMS token route router.post("/request-token-sms", asyncRouter(authController.RequestSms)); -// Logout route -//TODO: - -// Profile route -router.get( - "/profile", - authenticateUser, - asyncRouter(authController.getUserProfile) -); - export default router; diff --git a/src/routes/user.routes.ts b/src/routes/user.routes.ts new file mode 100644 index 00000000..b69bc2d0 --- /dev/null +++ b/src/routes/user.routes.ts @@ -0,0 +1,10 @@ +import express from "express"; +import { userController } from "../controllers/user.controller"; +import { asyncRouter } from "../utils/async-router.utils"; + +const router = express.Router(); + +// Profile route +router.get("/profile", asyncRouter(userController.getUserProfile)); + +export default router; diff --git a/src/server.ts b/src/server.ts index 5d9283b0..6758cdf3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -4,6 +4,7 @@ import express from "express"; import cors from "cors"; import helmet from "helmet"; import authRoutes from "./routes/auth.routes"; +import userRoutes from "./routes/user.routes"; import projectRoutes from "./routes/projects.routes"; import { errorMiddleware } from "./middlewares/error.middleware"; import loggerMiddleware from "./middlewares/logger.middleware"; @@ -29,6 +30,7 @@ try { // Routes app.use("/v2/auth", authRoutes); + app.use("/v2/user", authenticateUser, userRoutes); app.use("/v2/org/:orgId/project", authenticateUser, projectRoutes); //For unmatched route patterns diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 02a78428..ebcbf9c8 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -2,12 +2,7 @@ import { Request } from "express"; import { config } from "../config"; import { safePromise } from "../utils/index"; import https from "../utils/https.utils"; -import * as fs from "fs/promises"; -import { - LoginServiceType, - MigrationPayload, - UserProfile, -} from "../models/types"; +import { LoginServiceType, MigrationPayload } from "../models/types"; import { constants } from "../constants"; import { generateToken } from "../utils/jwt.utils"; import { @@ -116,48 +111,7 @@ const requestSms = async (req: Request): Promise => { } }; -const getUserProfile = async ( - req: Request -): Promise => { - try { - //TODO: replace the current logic with the actual db-fetch logic - const tokens = JSON.parse(await fs.readFile(`tokens.json`, "utf8")); - const authtoken = tokens?.[req?.headers?.app_token as string]; - - const apiResponse = - authtoken && - (await https({ - method: "GET", - url: `${config.CS_API.US}/user?include_orgs_roles=true`, - headers: { - "Content-Type": "application/json", - authtoken: authtoken, - }, - })); - - if (!apiResponse?.data?.user) - throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); - - const orgs = apiResponse?.data?.user?.organizations - ?.filter((org: any) => org?.org_roles?.some((item: any) => item.admin)) - ?.map(({ uid, name }: any) => ({ org_id: uid, org_name: name })); - - const userProfile: UserProfile = { - user: { - email: apiResponse?.data?.user?.email, - first_name: apiResponse?.data?.user?.first_name, - last_name: apiResponse?.data?.user?.last_name, - orgs: orgs, - }, - }; - return userProfile; - } catch (error) { - throw new InternalServerError("Error while getting user profile"); - } -}; - export const authService = { login, requestSms, - getUserProfile, }; diff --git a/src/services/user.service.ts b/src/services/user.service.ts new file mode 100644 index 00000000..538916fd --- /dev/null +++ b/src/services/user.service.ts @@ -0,0 +1,54 @@ +import { Request } from "express"; +import { config } from "../config"; +import https from "../utils/https.utils"; +import * as fs from "fs/promises"; +import { LoginServiceType, UserProfile } from "../models/types"; +import { constants } from "../constants"; +import { + BadRequestError, + InternalServerError, +} from "../utils/custom-errors.utils"; + +const getUserProfile = async ( + req: Request +): Promise => { + try { + //TODO: replace the current logic with the actual db-fetch logic + const tokens = JSON.parse(await fs.readFile(`tokens.json`, "utf8")); + const authtoken = tokens?.[req?.headers?.app_token as string]; + + const apiResponse = + authtoken && + (await https({ + method: "GET", + url: `${config.CS_API.US}/user?include_orgs_roles=true`, + headers: { + "Content-Type": "application/json", + authtoken: authtoken, + }, + })); + + if (!apiResponse?.data?.user) + throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); + + const orgs = apiResponse?.data?.user?.organizations + ?.filter((org: any) => org?.org_roles?.some((item: any) => item.admin)) + ?.map(({ uid, name }: any) => ({ org_id: uid, org_name: name })); + + const userProfile: UserProfile = { + user: { + email: apiResponse?.data?.user?.email, + first_name: apiResponse?.data?.user?.first_name, + last_name: apiResponse?.data?.user?.last_name, + orgs: orgs, + }, + }; + return userProfile; + } catch (error) { + throw new InternalServerError("Error while getting user profile"); + } +}; + +export const userService = { + getUserProfile, +}; From d582ac2c324b72f1b7f0b10244ccf56995c2d1ba Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 16:15:01 +0530 Subject: [PATCH 3/7] chore: updated the authenticateUser middleware to attach app_token's payload --- src/middlewares/auth.middleware.ts | 32 ++++++++++++++++-------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/src/middlewares/auth.middleware.ts b/src/middlewares/auth.middleware.ts index f8f739fe..ff28ab41 100644 --- a/src/middlewares/auth.middleware.ts +++ b/src/middlewares/auth.middleware.ts @@ -2,28 +2,30 @@ import { Request, Response, NextFunction } from "express"; import jwt from "jsonwebtoken"; import { config } from "../config"; +import { constants } from "../constants"; export const authenticateUser = ( req: Request, res: Response, next: NextFunction ) => { - // authentication logic (check for a valid token) + const status = constants.HTTP_CODES.UNAUTHORIZED; const token = req.get("app_token"); - if (token) { - jwt.verify(token, config.APP_TOKEN_KEY, (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; + if (!token) + return res + .status(status) + .json({ status, message: "Unauthorized - Token missing" }); - next(); - }); - } else { - return res.status(401).json({ message: "Unauthorized - Token missing" }); - } + jwt.verify(token, config.APP_TOKEN_KEY, (err, payload) => { + if (err) + return res + .status(status) + .json({ status, message: "Unauthorized - Invalid token" }); + + // Attach the payload to the request object for later use + (req as any).body.token_payload = payload; + + next(); + }); }; From 467d513891965ff20a30286ff2d21d5532b895db Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 16:56:02 +0530 Subject: [PATCH 4/7] fix: upsert logic added to login service to upsert the authtoken. --- src/models/types.ts | 2 +- src/services/auth.service.ts | 19 ++++++++++++------- src/utils/jwt.utils.ts | 4 ++-- 3 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/models/types.ts b/src/models/types.ts index 2f407966..5be9656e 100644 --- a/src/models/types.ts +++ b/src/models/types.ts @@ -12,7 +12,7 @@ export interface UserProfile { }; } -export interface MigrationPayload { +export interface AppTokenPayload { region: string; user_id: string; } diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index ebcbf9c8..80a08e17 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -2,7 +2,7 @@ import { Request } from "express"; import { config } from "../config"; import { safePromise } from "../utils/index"; import https from "../utils/https.utils"; -import { LoginServiceType, MigrationPayload } from "../models/types"; +import { LoginServiceType, AppTokenPayload } from "../models/types"; import { constants } from "../constants"; import { generateToken } from "../utils/jwt.utils"; import { @@ -50,19 +50,24 @@ const login = async (req: Request): Promise => { if (!res?.data?.user) throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); - const migration_payload: MigrationPayload = { + const appTokenPayload: AppTokenPayload = { region: userData?.region, user_id: res?.data?.user.uid, }; // Saving auth info in the DB - await AuthenticationModel.create({ - ...migration_payload, - authtoken: res?.data.user?.authtoken, - }); + await AuthenticationModel.findOneAndUpdate( + appTokenPayload, + { + authtoken: res?.data.user?.authtoken, + }, + { + upsert: true, + } + ); // JWT token generation - const app_token = generateToken(migration_payload); + const app_token = generateToken(appTokenPayload); return { data: { diff --git a/src/utils/jwt.utils.ts b/src/utils/jwt.utils.ts index 9639c443..9d1ce88c 100644 --- a/src/utils/jwt.utils.ts +++ b/src/utils/jwt.utils.ts @@ -1,10 +1,10 @@ /// src/utils/jwt.utils.ts import jwt from "jsonwebtoken"; -import { MigrationPayload } from "../models/types"; +import { AppTokenPayload } from "../models/types"; import { config } from "../config"; // @typescript-eslint/no-explicit-any -export const generateToken = (payload: MigrationPayload): string => { +export const generateToken = (payload: AppTokenPayload): string => { return jwt.sign(payload, config.APP_TOKEN_KEY, { expiresIn: config.APP_TOKEN_EXP, }); From 0dca071692babfd9db19d52d4a5062ae9d73925f Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 18:32:23 +0530 Subject: [PATCH 5/7] chore: DB query logic added the the user's service. --- src/controllers/user.controller.ts | 3 +- src/services/user.service.ts | 55 +++++++++++++++--------------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/controllers/user.controller.ts b/src/controllers/user.controller.ts index dbf58172..05c00271 100644 --- a/src/controllers/user.controller.ts +++ b/src/controllers/user.controller.ts @@ -1,9 +1,10 @@ import { Request, Response } from "express"; import { userService } from "../services/user.service"; +import { constants } from "../constants"; const getUserProfile = async (req: Request, res: Response) => { const user = await userService.getUserProfile(req); - res.status(200).json(user); + res.status(constants.HTTP_CODES.OK).json(user); }; export const userController = { diff --git a/src/services/user.service.ts b/src/services/user.service.ts index 538916fd..17e59b94 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -1,49 +1,50 @@ import { Request } from "express"; import { config } from "../config"; import https from "../utils/https.utils"; -import * as fs from "fs/promises"; -import { LoginServiceType, UserProfile } from "../models/types"; +import { AppTokenPayload, UserProfile } from "../models/types"; import { constants } from "../constants"; import { BadRequestError, InternalServerError, } from "../utils/custom-errors.utils"; +import AuthenticationModel from "../models/authentication"; -const getUserProfile = async ( - req: Request -): Promise => { +const getUserProfile = async (req: Request): Promise => { try { - //TODO: replace the current logic with the actual db-fetch logic - const tokens = JSON.parse(await fs.readFile(`tokens.json`, "utf8")); - const authtoken = tokens?.[req?.headers?.app_token as string]; - - const apiResponse = - authtoken && - (await https({ - method: "GET", - url: `${config.CS_API.US}/user?include_orgs_roles=true`, - headers: { - "Content-Type": "application/json", - authtoken: authtoken, - }, - })); - - if (!apiResponse?.data?.user) + const appTokenPayload: AppTokenPayload = req?.body?.token_payload; + + const user = await AuthenticationModel.findOne({ + user_id: appTokenPayload?.user_id, + region: appTokenPayload.region, + }).lean(); + + if (!user?.authtoken) + throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); + + const res = await https({ + method: "GET", + url: `${config.CS_API.US}/user?include_orgs_roles=true`, + headers: { + "Content-Type": "application/json", + authtoken: user?.authtoken, + }, + }); + + if (!res?.data?.user) throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); - const orgs = apiResponse?.data?.user?.organizations + const orgs = (res?.data?.user?.organizations || []) ?.filter((org: any) => org?.org_roles?.some((item: any) => item.admin)) ?.map(({ uid, name }: any) => ({ org_id: uid, org_name: name })); - const userProfile: UserProfile = { + return { user: { - email: apiResponse?.data?.user?.email, - first_name: apiResponse?.data?.user?.first_name, - last_name: apiResponse?.data?.user?.last_name, + email: res?.data?.user?.email, + first_name: res?.data?.user?.first_name, + last_name: res?.data?.user?.last_name, orgs: orgs, }, }; - return userProfile; } catch (error) { throw new InternalServerError("Error while getting user profile"); } From 4b46c31fde438dce1e9cfabaa7192dda8cc257e5 Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 18:48:37 +0530 Subject: [PATCH 6/7] fix: removed try..catch block as it's not required, we've asyncRouter. --- src/services/auth.service.ts | 108 +++++++++++++++++------------------ src/services/user.service.ts | 77 ++++++++++++------------- 2 files changed, 87 insertions(+), 98 deletions(-) diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 80a08e17..ca89ba42 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -15,70 +15,66 @@ const login = async (req: Request): Promise => { //TODO: 1. request validation const userData = req?.body; - try { - const [err, res] = await safePromise( - https({ - method: "POST", - url: `${config.CS_API[ - userData?.region as keyof typeof config.CS_API - ]!}/user-session`, - headers: { - "Content-Type": "application/json", - }, - data: { - user: { - email: userData?.email, - password: userData?.password, - ...(userData?.tfa_token && { tfa_token: userData?.tfa_token }), - }, + const [err, res] = await safePromise( + https({ + method: "POST", + url: `${config.CS_API[ + userData?.region as keyof typeof config.CS_API + ]!}/user-session`, + headers: { + "Content-Type": "application/json", + }, + data: { + user: { + email: userData?.email, + password: userData?.password, + ...(userData?.tfa_token && { tfa_token: userData?.tfa_token }), }, - }) - ); + }, + }) + ); - if (err) - return { - data: err?.response?.data, - status: err?.response?.status, - }; + if (err) + return { + data: err?.response?.data, + status: err?.response?.status, + }; - if (res?.status === constants.HTTP_CODES.SUPPORT_DOC) - return { - data: res?.data, - status: res?.status, - }; + if (res?.status === constants.HTTP_CODES.SUPPORT_DOC) + return { + data: res?.data, + status: res?.status, + }; - if (!res?.data?.user) - throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); + if (!res?.data?.user) + throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); - const appTokenPayload: AppTokenPayload = { - region: userData?.region, - user_id: res?.data?.user.uid, - }; + const appTokenPayload: AppTokenPayload = { + region: userData?.region, + user_id: res?.data?.user.uid, + }; - // Saving auth info in the DB - await AuthenticationModel.findOneAndUpdate( - appTokenPayload, - { - authtoken: res?.data.user?.authtoken, - }, - { - upsert: true, - } - ); + // Saving auth info in the DB + await AuthenticationModel.findOneAndUpdate( + appTokenPayload, + { + authtoken: res?.data.user?.authtoken, + }, + { + upsert: true, + } + ); - // JWT token generation - const app_token = generateToken(appTokenPayload); + // JWT token generation + const app_token = generateToken(appTokenPayload); - return { - data: { - message: constants.HTTP_TEXTS.SUCCESS_LOGIN, - app_token, - }, - status: constants.HTTP_CODES.OK, - }; - } catch (err) { - throw new InternalServerError(); - } + return { + data: { + message: constants.HTTP_TEXTS.SUCCESS_LOGIN, + app_token, + }, + status: constants.HTTP_CODES.OK, + }; }; const requestSms = async (req: Request): Promise => { diff --git a/src/services/user.service.ts b/src/services/user.service.ts index 17e59b94..2a410e3c 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -3,51 +3,44 @@ import { config } from "../config"; import https from "../utils/https.utils"; import { AppTokenPayload, UserProfile } from "../models/types"; import { constants } from "../constants"; -import { - BadRequestError, - InternalServerError, -} from "../utils/custom-errors.utils"; +import { BadRequestError } from "../utils/custom-errors.utils"; import AuthenticationModel from "../models/authentication"; const getUserProfile = async (req: Request): Promise => { - try { - const appTokenPayload: AppTokenPayload = req?.body?.token_payload; - - const user = await AuthenticationModel.findOne({ - user_id: appTokenPayload?.user_id, - region: appTokenPayload.region, - }).lean(); - - if (!user?.authtoken) - throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); - - const res = await https({ - method: "GET", - url: `${config.CS_API.US}/user?include_orgs_roles=true`, - headers: { - "Content-Type": "application/json", - authtoken: user?.authtoken, - }, - }); - - if (!res?.data?.user) - throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); - - const orgs = (res?.data?.user?.organizations || []) - ?.filter((org: any) => org?.org_roles?.some((item: any) => item.admin)) - ?.map(({ uid, name }: any) => ({ org_id: uid, org_name: name })); - - return { - user: { - email: res?.data?.user?.email, - first_name: res?.data?.user?.first_name, - last_name: res?.data?.user?.last_name, - orgs: orgs, - }, - }; - } catch (error) { - throw new InternalServerError("Error while getting user profile"); - } + const appTokenPayload: AppTokenPayload = req?.body?.token_payload; + + const user = await AuthenticationModel.findOne({ + user_id: appTokenPayload?.user_id, + region: appTokenPayload.region, + }).lean(); + + if (!user?.authtoken) + throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); + + const res = await https({ + method: "GET", + url: `${config.CS_API.US}/user?include_orgs_roles=true`, + headers: { + "Content-Type": "application/json", + authtoken: user?.authtoken, + }, + }); + + if (!res?.data?.user) + throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER); + + const orgs = (res?.data?.user?.organizations || []) + ?.filter((org: any) => org?.org_roles?.some((item: any) => item.admin)) + ?.map(({ uid, name }: any) => ({ org_id: uid, org_name: name })); + + return { + user: { + email: res?.data?.user?.email, + first_name: res?.data?.user?.first_name, + last_name: res?.data?.user?.last_name, + orgs: orgs, + }, + }; }; export const userService = { From 7459ffdc8fbd1226b27a1d7e5f85b0cfbd337d41 Mon Sep 17 00:00:00 2001 From: hanoak20 Date: Fri, 19 Jan 2024 18:52:19 +0530 Subject: [PATCH 7/7] fix: added dynamic region delection to user's service. --- src/services/user.service.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/services/user.service.ts b/src/services/user.service.ts index 2a410e3c..89110f20 100644 --- a/src/services/user.service.ts +++ b/src/services/user.service.ts @@ -11,7 +11,7 @@ const getUserProfile = async (req: Request): Promise => { const user = await AuthenticationModel.findOne({ user_id: appTokenPayload?.user_id, - region: appTokenPayload.region, + region: appTokenPayload?.region, }).lean(); if (!user?.authtoken) @@ -19,7 +19,9 @@ const getUserProfile = async (req: Request): Promise => { const res = await https({ method: "GET", - url: `${config.CS_API.US}/user?include_orgs_roles=true`, + url: `${config.CS_API[ + appTokenPayload?.region as keyof typeof config.CS_API + ]!}/user?include_orgs_roles=true`, headers: { "Content-Type": "application/json", authtoken: user?.authtoken,