Skip to content

Commit

Permalink
fix: move code into try
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-uhryn-absa committed Dec 14, 2023
1 parent ade5d9e commit 1d9e98c
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/backend/src/auth/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ export function authenticate(

try {
verification = jwt.verify(token, secretKey)

if (typeof verification === 'string') {
next(
new Error(
`Malformed JWT token: received 'string', expected 'object'`,
),
)
} else {
;(req as RequestWithToken).jwtPayload = verification
next()
}
} catch (error) {
if (error instanceof TokenExpiredError) {
res.status(403).json({ error: 'Your token has expired.' })
Expand All @@ -33,15 +44,6 @@ export function authenticate(
res.status(403).json({ error: 'Unknown token error.' })
}
}

if (typeof verification === 'string') {
next(
new Error(`Malformed JWT token: received 'string', expected 'object'`),
)
} else {
;(req as RequestWithToken).jwtPayload = verification
next()
}
} else {
res.status(403).json({ error: 'You are not logged in.' })
}
Expand Down

0 comments on commit 1d9e98c

Please sign in to comment.