From b2e6988103277cc48c904528ec102db5bfa574f5 Mon Sep 17 00:00:00 2001 From: Arpit1119 Date: Thu, 4 Jan 2024 15:08:46 +0530 Subject: [PATCH] feat: add endpoint to get user profile and orgs --- src/controllers/auth.controller.ts | 10 ++++++ src/models/types.ts | 9 ++++++ src/routes/auth.routes.ts | 5 +++ src/services/auth.service.ts | 52 +++++++++++++++++++++++++++++- 4 files changed, 75 insertions(+), 1 deletion(-) diff --git a/src/controllers/auth.controller.ts b/src/controllers/auth.controller.ts index 411af0755..9759dc8bd 100644 --- a/src/controllers/auth.controller.ts +++ b/src/controllers/auth.controller.ts @@ -9,7 +9,17 @@ const login = async (req: Request, res: Response) => { res.status(500).json({ message: "Internal Server Error" }); } }; +const getUserProfile = async (req: Request, res: Response) => { + try { + const user = await userService.getUserProfile(req); + res.status(200).json(user); + } catch (error) { + console.error(error); + res.status(500).json({ message: "Internal Server Error" }); + } +}; export const authController = { login, + getUserProfile, }; diff --git a/src/models/types.ts b/src/models/types.ts index c5fc46bd1..7c27c42a9 100644 --- a/src/models/types.ts +++ b/src/models/types.ts @@ -3,6 +3,15 @@ export interface User { password: string; } +export interface UserProfile { + user: { + email: string; + first_name: string; + last_name: string; + orgs: [{ org_id: string; org_name: string }]; + }; +} + export interface ResponseType { message: string; status: number; diff --git a/src/routes/auth.routes.ts b/src/routes/auth.routes.ts index 6bac4b7e2..8d08b970a 100644 --- a/src/routes/auth.routes.ts +++ b/src/routes/auth.routes.ts @@ -7,5 +7,10 @@ const router = express.Router(); // Login route router.post("/login", authenticateUser, asyncRouter(authController.login)); +router.get( + "/profile", + authenticateUser, + asyncRouter(authController.getUserProfile) +); export default router; diff --git a/src/services/auth.service.ts b/src/services/auth.service.ts index 46aee53ed..51519105f 100644 --- a/src/services/auth.service.ts +++ b/src/services/auth.service.ts @@ -3,7 +3,7 @@ import { config } from "../config"; import { generateToken } from "../utils/jwt.utils"; import https from "../utils/https.utils"; import * as fs from "fs/promises"; -import { MigrationPayload, ResponseType } from "../models/types"; +import { MigrationPayload, ResponseType, UserProfile } from "../models/types"; const login = async (req: Request): Promise => { const userData = req.body; @@ -54,6 +54,56 @@ const login = async (req: Request): Promise => { } }; +const getUserProfile = async ( + req: Request +): Promise => { + try { + 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 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; + } + return { + message: "Invalid User", + status: 401, + migration_token: null, + }; + } catch (error) { + // Handle errors (e.g., log, return an error response) + console.error(error); + return { + message: "Error while getting user profile", + status: 500, + migration_token: null, + }; + } +}; + export const userService = { login, + getUserProfile, };