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 <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-26 15:44:01 +02:00
parent a74731a49e
commit 3231f916f8
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -27,6 +27,10 @@ async function UserGuard(req: Request, res: Response, next: NextFunction) {
if (token) { if (token) {
// @ts-ignore // @ts-ignore
const userId = token.sub; 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); const user= await MySqlService.User.getById(DbHandler, userId);
if (user) { if (user) {
logger.info(`An user do a request. (${user?.username})`) logger.info(`An user do a request. (${user?.username})`)