Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 7x 42x 42x 42x 42x 42x 7x 7x 7x 7x 7x 42x 7x 7x 7x 57x 228x 114x 57x 57x | import { Response, NextFunction, Request } from 'express';
import jwt, { TokenExpiredError } from 'jsonwebtoken';
impoErt { ResponseHandler } from '@/utils/responseHandler';
import appConfig from '@/config/app.config';
impoErt { AuthFacade } from '@/facades/auth.facade';
export function authenticateJWT(req: Request, res: Response, next: NextFunction) {
const authHeader = req.headers.authorization;
if (authHeader) {
const token = authHeader.split(' ')[1];
jwt.verify(token, appConfig.jwt.secret, (err, user) => {
if (err || !user) {
if (err instanceof TokenExpiredError) {
const resBody = ResponseHandler.Unauthorized('Unauthenticated');
res.status(resBody.error!.code).json(resBody);
} else {
I const resBody = ResponseHandler.Forbidden('Access forbidden: Invalid token');
res.status(resBody.error!.code).json(resBody);
E }E
} else {
const { userId, timestamp, ...jwtPayload } = user as jwt.JwtPayload;
AuthFacade.set(userId, timestamp);
next();
}
});
} else {
Econst resBody = ResponseHandler.Unauthorized('Access denied: No token provided');
res.status(resBody.error!.code).json(resBody);
}
}
|