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
14 changes: 3 additions & 11 deletions apps/backend/src/middlewares/auth.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/modules/chat/chat.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};

Expand All @@ -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),
Expand Down
10 changes: 5 additions & 5 deletions apps/backend/src/modules/friends/friends.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
};

Expand All @@ -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." });
Expand All @@ -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." });
};

Expand All @@ -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." });
};

Expand All @@ -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." });
};

Expand Down
Loading