From 3231f916f816f435ebd530d43eba989a22e1bbe1 Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 26 Apr 2024 15:44:01 +0200 Subject: [PATCH] feat(validators): add check for non-existent userId - The `UserGuard` now checks whether the `userId` extracted from the token exists. - If the `userId` does not exist, an error is logged and a response with the 'Unauthorized' status is returned. Signed-off-by: Mathis --- src/validators/UserGuard.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/validators/UserGuard.ts b/src/validators/UserGuard.ts index 033ad6a..7d87ca3 100644 --- a/src/validators/UserGuard.ts +++ b/src/validators/UserGuard.ts @@ -27,6 +27,10 @@ async function UserGuard(req: Request, res: Response, next: NextFunction) { if (token) { // @ts-ignore const userId = token.sub; + if (!userId) { + logger.error(USER_NOT_EXIST); + return res.status(UNAUTHORIZED).json({message: USER_NOT_EXIST}); + } const user= await MySqlService.User.getById(DbHandler, userId); if (user) { logger.info(`An user do a request. (${user?.username})`)