From 1af696b39c84e7f395d25f503e8950dcf616394d Mon Sep 17 00:00:00 2001 From: Methapon2001 <61303214+Methapon2001@users.noreply.github.com> Date: Mon, 11 Mar 2024 09:30:28 +0700 Subject: [PATCH] Add custom error msg param for role auth --- src/middlewares/role.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/middlewares/role.ts b/src/middlewares/role.ts index f6e6da9d..0ea1125b 100644 --- a/src/middlewares/role.ts +++ b/src/middlewares/role.ts @@ -3,10 +3,10 @@ import HttpError from "../interfaces/http-error"; import HttpStatus from "../interfaces/http-status"; import { RequestWithUser } from "./user"; -export function authRole(role: string) { +export function authRole(role: string, errorMessage = "คุณไม่มีสิทธิในการเข้าถึงทรัพยากรดังกล่าว") { return (req: RequestWithUser, _res: express.Response, next: express.NextFunction) => { if (!req.user.role.includes(role)) { - throw new HttpError(HttpStatus.FORBIDDEN, "คุณไม่มีสิทธิในการเข้าถึงทรัพยากรดังกล่าว"); + throw new HttpError(HttpStatus.FORBIDDEN, errorMessage); } next(); };