hrms-api-org/src/middlewares/role.ts

14 lines
582 B
TypeScript
Raw Normal View History

2024-03-11 09:28:59 +07:00
import * as express from "express";
import HttpError from "../interfaces/http-error";
import HttpStatus from "../interfaces/http-status";
import { RequestWithUser } from "./user";
export function authRole(role: string, errorMessage = "คุณไม่มีสิทธิในการเข้าถึงทรัพยากรดังกล่าว") {
2024-03-11 09:28:59 +07:00
return (req: RequestWithUser, _res: express.Response, next: express.NextFunction) => {
if (!req.user.role.includes(role)) {
throw new HttpError(HttpStatus.FORBIDDEN, errorMessage);
2024-03-11 09:28:59 +07:00
}
next();
};
}