You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
810 B
JavaScript

import ApiError from '../controllers/ErrorController.js';
import TokenService from '../services/TokenService.js';
export default function (req, res, next) {
if(req.method === 'OPTIONS') {
next();
}
try {
const authHeader = req.headers.authorization;
if(!authHeader) {
return next(ApiError.UnauthorizedError());
}
const accessToken = authHeader.split(' ')[1];
if(!accessToken) {
return next(ApiError.UnauthorizedError());
}
const decodedData = TokenService.validateAccessToken(accessToken);
if(!decodedData) {
return next(ApiError.UnauthorizedError());
}
req.user = decodedData;
next();
} catch (e) {
return next(ApiError.UnauthorizedError());
}
}