lecoffre-back/src/app/middlewares/RolesHandler.ts
2023-10-24 10:24:03 +02:00

28 lines
752 B
TypeScript

import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
import { NextFunction, Request, Response } from "express";
export default async function roleHandler(req: Request, response: Response, next: NextFunction) {
try {
const namespace = req.path && req.path.split("/")[3];
const role = req.body.user.role;
if(!role) {
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized without role");
return;
}
if (namespace != "notary" && role != namespace && role != "super-admin") {
response.status(HttpCodes.UNAUTHORIZED).send("Unauthorized with this role");
return;
}
next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
}