diff --git a/src/controllers/AuthenticationController.ts b/src/controllers/AuthenticationController.ts index ef4ddba..0f80b12 100644 --- a/src/controllers/AuthenticationController.ts +++ b/src/controllers/AuthenticationController.ts @@ -34,7 +34,7 @@ export class AuthenticationController { return user } catch (error: any) { - throw new Error('Invalid token') + throw new Error(`Invalid token. ${error.message}`) } } } diff --git a/src/middleware/injectUserMiddleware.ts b/src/middleware/injectUserMiddleware.ts index 0d7f455..bb6efe7 100644 --- a/src/middleware/injectUserMiddleware.ts +++ b/src/middleware/injectUserMiddleware.ts @@ -21,8 +21,12 @@ export async function injectUserMiddleware( const token = getToken(req) if (token) { - const user = await AuthenticationController.authenticate(token) - req.user = user + try { + const user = await AuthenticationController.authenticate(token) + req.user = user + } catch (error: any) { + return res.status(401).json({ error: error.message }) + } } next()