Add middleware for role check
This commit is contained in:
parent
610777e48d
commit
6ba483f0b7
2 changed files with 25 additions and 0 deletions
13
src/middlewares/role.ts
Normal file
13
src/middlewares/role.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
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) {
|
||||
return (req: RequestWithUser, _res: express.Response, next: express.NextFunction) => {
|
||||
if (!req.user.role.includes(role)) {
|
||||
throw new HttpError(HttpStatus.FORBIDDEN, "คุณไม่มีสิทธิในการเข้าถึงทรัพยากรดังกล่าว");
|
||||
}
|
||||
next();
|
||||
};
|
||||
}
|
||||
12
src/middlewares/user.ts
Normal file
12
src/middlewares/user.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
import type { Request } from "express";
|
||||
|
||||
export type RequestWithUser = Request & {
|
||||
user: {
|
||||
name: string;
|
||||
given_name: string;
|
||||
familiy_name: string;
|
||||
preferred_username: string;
|
||||
email: string;
|
||||
role: string[];
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue