From 254bc07da744a9708109bfb792be70aa3f6284f4 Mon Sep 17 00:00:00 2001 From: Sabir Hassan Date: Tue, 2 Aug 2022 23:05:42 +0500 Subject: [PATCH] fix: call jwt.verify in synchronous way --- api/src/middlewares/authenticateToken.ts | 33 +++++++++++------------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/api/src/middlewares/authenticateToken.ts b/api/src/middlewares/authenticateToken.ts index dc1f42e..78e4bcb 100644 --- a/api/src/middlewares/authenticateToken.ts +++ b/api/src/middlewares/authenticateToken.ts @@ -93,27 +93,24 @@ const authenticateToken = async ( try { if (!token) throw 'Unauthorized' - jwt.verify(token, key, async (err: any, data: any) => { - if (err) throw 'Unauthorized' + const data: any = jwt.verify(token, key) - // verify this valid token's entry in DB - const user = await verifyTokenInDB( - data?.userId, - data?.clientId, - token, - tokenType - ) + const user = await verifyTokenInDB( + data?.userId, + data?.clientId, + token, + tokenType + ) - if (user) { - if (user.isActive) { - req.user = user - if (tokenType === 'accessToken') req.accessToken = token - return next() - } else throw 'Unauthorized' - } + if (user) { + if (user.isActive) { + req.user = user + if (tokenType === 'accessToken') req.accessToken = token + return next() + } else throw 'Unauthorized' + } - throw 'Unauthorized' - }) + throw 'Unauthorized' } catch (error) { if (await isPublicRoute(req)) { req.user = publicUser