lecoffre-back/src/app/middlewares/AuthHandler.ts
omaroughriss 3e5d17157b
All checks were successful
Test - Build & Deploy to Scaleway / build-and-push-images-lecoffre (push) Successful in 1m48s
Test - Build & Deploy to Scaleway / deploy-back-lecoffre (push) Successful in 4s
Test - Build & Deploy to Scaleway / deploy-cron-lecoffre (push) Successful in 3s
Add logs while verifying token
2025-07-31 11:27:13 +02:00

31 lines
1.0 KiB
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) {
try {
const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1];
if (!token) {
response.status(HttpCodes.UNAUTHORIZED).send("Missing token in authorization header");
return;
}
const authService = Container.get(AuthService);
authService.verifyAccessToken(token, (err, userPayload) => {
if (err) {
response.status(HttpCodes.UNAUTHORIZED).send("Error while verifying token:" + JSON.stringify(err) + " token:" + JSON.stringify(token));
return;
}
req.body.user = userPayload;
next();
});
} catch (error) {
console.error(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
}