UNPKG

typescript-express-starter

Version:
40 lines (33 loc) 1.25 kB
import { NextFunction, Response } from 'express'; import { verify } from 'jsonwebtoken'; import { SECRET_KEY } from '@config'; import { DI } from '@database'; import { HttpException } from '@exceptions/httpException'; import { DataStoredInToken, RequestWithUser } from '@interfaces/auth.interface'; const getAuthorization = (req) => { const coockie = req.cookies['Authorization']; if (coockie) return coockie; const header = req.header('Authorization'); if (header) return header.split('Bearer ')[1]; return null; } export const AuthMiddleware = async (req: RequestWithUser, res: Response, next: NextFunction) => { try { const Authorization = getAuthorization(req); if (Authorization) { const verificationResponse = verify(Authorization, SECRET_KEY) as DataStoredInToken; const { _id } = verificationResponse; const findUser = await DI.userRepository.findOne(_id); if (findUser) { req.user = findUser; next(); } else { next(new HttpException(401, 'Wrong authentication token')); } } else { next(new HttpException(404, 'Authentication token missing')); } } catch (error) { next(new HttpException(401, 'Wrong authentication token')); } };