diff --git a/apps/backend/src/middlewares/auth.ts b/apps/backend/src/middlewares/auth.ts index d84560a..d6ebf3e 100644 --- a/apps/backend/src/middlewares/auth.ts +++ b/apps/backend/src/middlewares/auth.ts @@ -1,12 +1,9 @@ import { Request, Response, NextFunction } from "express"; import { AuthError } from "@repo/backend/utils/errors"; import { jwtVerifyAccessToken } from "@repo/backend/utils/jwt"; -import { User, users } from "@repo/database/schema"; -import { db } from "@repo/database"; -import { eq } from "drizzle-orm"; export interface AuthenticatedRequest extends Request { - user: User; + userId: number; } const authHandler = async (req: Request, res: Response, next: NextFunction) => { @@ -20,15 +17,10 @@ const authHandler = async (req: Request, res: Response, next: NextFunction) => { const { userId } = jwtVerifyAccessToken(token); if (!userId) throw new AuthError("Invalid token."); - const user = await db.query.users.findFirst({ - where: eq(users.id, userId), - }); - if (!user) throw new AuthError("Invalid token."); - - (req as AuthenticatedRequest).user = user; + (req as AuthenticatedRequest).userId = userId; } catch (error: unknown) { if (error instanceof Error) throw new AuthError(error.message); - else throw new AuthError("Invalid token"); + else throw new AuthError("Invalid token."); } next(); diff --git a/apps/backend/src/modules/chat/chat.controller.ts b/apps/backend/src/modules/chat/chat.controller.ts index d3c809a..fc971e3 100644 --- a/apps/backend/src/modules/chat/chat.controller.ts +++ b/apps/backend/src/modules/chat/chat.controller.ts @@ -4,7 +4,7 @@ import { getChats, getChatDetails } from "./chat.service"; import { AuthenticatedRequest } from "@repo/backend/middlewares/auth"; const getChatsController = async (req: AuthenticatedRequest, res: Response) => { - const chats = await getChats(req.user.id); + const chats = await getChats(req.userId); res.status(StatusCodes.OK).json({ chats }); }; @@ -15,7 +15,7 @@ const getChatDetailsController = async ( const { chatId } = req.params; const { page, limit } = req.query; const chatDetails = await getChatDetails( - req.user.id, + req.userId, parseInt(chatId), parseInt(page as string), parseInt(limit as string), diff --git a/apps/backend/src/modules/friends/friends.controller.ts b/apps/backend/src/modules/friends/friends.controller.ts index 02e844e..635d346 100644 --- a/apps/backend/src/modules/friends/friends.controller.ts +++ b/apps/backend/src/modules/friends/friends.controller.ts @@ -13,7 +13,7 @@ const getFriendsController = async ( req: AuthenticatedRequest, res: Response, ) => { - const friends = await getFriends(req.user.id); + const friends = await getFriends(req.userId); res.status(StatusCodes.OK).json({ friends }); }; @@ -22,7 +22,7 @@ const addFriendController = async ( res: Response, ) => { const { friendId } = req.params; - await addFriend(req.user.id, parseInt(friendId)); + await addFriend(req.userId, parseInt(friendId)); res .status(StatusCodes.OK) .json({ message: "Friend request sent successfully." }); @@ -33,7 +33,7 @@ const deleteFriendController = async ( res: Response, ) => { const { friendId } = req.params; - await deleteFriend(req.user.id, parseInt(friendId)); + await deleteFriend(req.userId, parseInt(friendId)); res.status(StatusCodes.OK).json({ message: "Friend deleted successfully." }); }; @@ -42,7 +42,7 @@ const acceptFriendRequestController = async ( res: Response, ) => { const { friendId } = req.params; - await acceptFriendRequest(req.user.id, parseInt(friendId)); + await acceptFriendRequest(req.userId, parseInt(friendId)); res.status(StatusCodes.OK).json({ message: "Friend request accepted." }); }; @@ -51,7 +51,7 @@ const denyFriendRequestController = async ( res: Response, ) => { const { friendId } = req.params; - await denyFriendRequest(req.user.id, parseInt(friendId)); + await denyFriendRequest(req.userId, parseInt(friendId)); res.status(StatusCodes.OK).json({ message: "Friend request denied." }); };