improvement: better token error handling

This commit is contained in:
canove
2020-09-24 14:08:57 -03:00
parent 34a99ade2d
commit f6b5ed389a

View File

@@ -21,13 +21,17 @@ const isAuth = (req: Request, res: Response, next: NextFunction): void => {
const [, token] = authHeader.split(" "); const [, token] = authHeader.split(" ");
const decoded = verify(token, authConfig.secret); try {
const { id, profile } = decoded as TokenPayload; const decoded = verify(token, authConfig.secret);
const { id, profile } = decoded as TokenPayload;
req.user = { req.user = {
id, id,
profile profile
}; };
} catch (err) {
throw new AppError("Invalid token.", 403);
}
return next(); return next();
}; };