import jwt from 'jsonwebtoken'; import ApiError from '../controllers/ErrorController.js'; import TokenService from '../services/TokenService.js'; import config from "config"; 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()); } }