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
10 changes: 10 additions & 0 deletions src/controllers/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
9 changes: 9 additions & 0 deletions src/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions src/routes/auth.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
52 changes: 51 additions & 1 deletion src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ResponseType | null> => {
const userData = req.body;
Expand Down Expand Up @@ -54,6 +54,56 @@ const login = async (req: Request): Promise<ResponseType | null> => {
}
};

const getUserProfile = async (
req: Request
): Promise<UserProfile | ResponseType> => {
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,
};