fosrl.pangolin/server/routers/user/getUser.ts

51 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-10-01 21:34:07 -04:00
import { Request, Response, NextFunction } from 'express';
2024-10-01 21:53:49 -04:00
import { z } from 'zod';
import { db } from '@server/db';
import { users } from '@server/db/schema';
import { eq } from 'drizzle-orm';
2024-10-01 21:34:07 -04:00
import response from "@server/utils/response";
import HttpCode from '@server/types/HttpCode';
2024-10-01 21:53:49 -04:00
import createHttpError from 'http-errors';
2024-10-06 16:43:59 -04:00
import { ActionsEnum, checkUserActionPermission } from '@server/auth/actions';
2024-10-06 18:05:20 -04:00
import logger from '@server/logger';
2024-10-01 21:53:49 -04:00
2024-10-01 21:34:07 -04:00
export async function getUser(req: Request, res: Response, next: NextFunction): Promise<any> {
2024-10-06 18:05:20 -04:00
try {
2024-10-06 18:43:20 -04:00
const userId = req.user?.id;
if (!userId) {
return next(createHttpError(HttpCode.UNAUTHORIZED, "User not found"));
2024-10-06 18:05:20 -04:00
}
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
const user = await db.select()
.from(users)
.where(eq(users.id, userId))
.limit(1);
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
if (user.length === 0) {
return next(
createHttpError(
HttpCode.NOT_FOUND,
`User with ID ${userId} not found`
)
);
}
2024-10-01 21:53:49 -04:00
2024-10-06 18:05:20 -04:00
return response(res, {
2024-10-06 18:43:20 -04:00
data: {
email: user[0].email,
twoFactorEnabled: user[0].twoFactorEnabled,
emailVerified: user[0].emailVerified
},
2024-10-06 18:05:20 -04:00
success: true,
error: false,
message: "User retrieved successfully",
status: HttpCode.OK,
});
} catch (error) {
logger.error(error);
return next(createHttpError(HttpCode.INTERNAL_SERVER_ERROR, "An error occurred..."));
}
}