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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 1x 1x 1x 4x 4x 4x 5x 1x 1x 1x 3x 3x 3x 2x 5x 2x 2x 2x 2x 5x 1x 1x 5x 1x 1x 1x 1x | // auth.ts
import {Response, NextFunction, RequestHandler} from 'express';
import jwt from 'jsonwebtoken';
import config from '../config/config';
import { CustomRequest } from 'types/customRequest';
import {unless} from 'express-unless';
import {UserData} from "types/user_types";
import * as usersService from '../services/users_service';
// Middleware to authenticate token for all requests
/**
* @swagger
* components:
* securitySchemes:
* BearerAuth:
* type: http
* scheme: bearer
* bearerFormat: JWT
*
* security:
* - BearerAuth: []
*/
const authenticateToken: any & { unless: typeof unless } = async (req: CustomRequest, res: Response, next: NextFunction): Promise<void> => {
const token = req.headers['authorization']?.split(' ')[1];
if (!token) {
res.status(401).json({ message: 'Access token required' });
return;
}
try {
const isBlacklisted = await usersService.isAccessTokenBlacklisted(token);
if (isBlacklisted) {
res.status(403).json({ message: 'Token is blacklisted' });
return;
}
const decoded = jwt.verify(token, config.auth.access_token) as jwt.JwtPayload;
const user = await usersService.getUserById(decoded.userId);
if (!user) {
res.status(403).json({ message: 'Invalid token' });
return;
}
req.user = user;
next();
} catch (err) {
res.status(403).json({ message: 'Invalid token' });
}
};
authenticateToken.unless = unless;
export default authenticateToken; |