Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
feat(authenticatejwt): add userObj to res.locals
Browse files Browse the repository at this point in the history
  • Loading branch information
GGORG0 committed Jun 29, 2022
1 parent e8f0fda commit 7259431
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/routes/authRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,25 @@ export function authenticateJWT(req: Request, res: Response, next: NextFunction)
!(typeof decoded.user === "string") ||
!(typeof decoded.type === "string") ||
!(decoded.type === "auth"))
return res.status(403).json({ success: false, error: "Invalid token" });
res.locals.user = decoded.user;
next();
return res.status(401).json({ success: false, error: "Invalid token" });
prisma.user.findUnique({ where: { uuid: decoded.user } }).then(user => {
if (!user) {
return res.status(401).json({ success: false, error: "Invalid user" });
}

res.locals.user = user.uuid;
res.locals.userObj = user;
next();
}).catch((e: unknown) => {
res.status(500).json({ success: false, error: e });
});
}
catch (err) {
if (typeof err === typeof TokenExpiredError) {
return res.status(403).json({ success: false, error: "Token expired" });
}
else {
return res.status(403).json({ success: false, error: "Invalid token" });
return res.status(401).json({ success: false, error: "Invalid token" });
}
}

Expand Down

0 comments on commit 7259431

Please sign in to comment.