26 lines
761 B
TypeScript
26 lines
761 B
TypeScript
import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
|
|
import AuthService from "@Services/common/AuthService/AuthService";
|
|
import { NextFunction, Request, Response } from "express";
|
|
import Container from "typedi";
|
|
|
|
export default function authHandler(req: Request, response: Response, next: NextFunction) {
|
|
const authHeader = req.headers['authorization'];
|
|
const token = authHeader && authHeader.split(' ')[1];
|
|
|
|
if (!token) {
|
|
response.sendStatus(HttpCodes.UNAUTHORIZED)
|
|
return;
|
|
}
|
|
|
|
const authService = Container.get(AuthService);
|
|
authService.verifyAccessToken(token, (err, userPayload) => {
|
|
if (err) {
|
|
response.sendStatus(HttpCodes.UNAUTHORIZED);
|
|
return;
|
|
}
|
|
req.body.user = userPayload
|
|
next();
|
|
});
|
|
}
|
|
|