-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #80 from abinth11/bug-fixes-and-updates
role check middleware updated
- Loading branch information
Showing
6 changed files
with
28 additions
and
69 deletions.
There are no files selected for viewing
Empty file.
59 changes: 11 additions & 48 deletions
59
server/src/frameworks/webserver/middlewares/roleCheckMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,17 @@ | ||
// Admin Role Middleware | ||
import { CustomRequest } from '../../../types/customRequest'; | ||
import { NextFunction,Response } from 'express'; | ||
import { CustomRequest } from '../../../types/customRequest'; | ||
import AppError from '../../../utils/appError'; | ||
import HttpStatusCodes from '../../../constants/HttpStatusCodes'; | ||
export const adminRoleCheckMiddleware = ( | ||
req: CustomRequest, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const role = req.user?.role; | ||
if (role === 'admin') { | ||
// User has the admin role, allow access | ||
next(); | ||
} else { | ||
// User does not have the admin role, deny access | ||
throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED); | ||
} | ||
}; | ||
|
||
// Instructor Role Middleware | ||
export const instructorRoleCheckMiddleware = ( | ||
req: CustomRequest, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const role = req.user?.role | ||
|
||
|
||
if (role === 'instructor') { | ||
// User has the instructor role, allow access | ||
next(); | ||
} else { | ||
// User does not have the instructor role, deny access | ||
throw new AppError('Unauthorized role, you are not a instructor', HttpStatusCodes.UNAUTHORIZED); | ||
} | ||
const roleCheckMiddleware = (roleToCheck: string) => { | ||
return (req: CustomRequest, res: Response, next: NextFunction) => { | ||
const role = req.user?.role; | ||
if (role === roleToCheck) { | ||
next(); | ||
} else { | ||
throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED); | ||
} | ||
}; | ||
}; | ||
|
||
export const studentRoleCheckMiddleware = ( | ||
req: CustomRequest, | ||
res: Response, | ||
next: NextFunction | ||
) => { | ||
const role = req.user?.role; | ||
|
||
|
||
if (role === 'student') { | ||
// User has the instructor role, allow access | ||
next(); | ||
} else { | ||
// User does not have the instructor role, deny access | ||
throw new AppError('Unauthorized role', HttpStatusCodes.UNAUTHORIZED); | ||
} | ||
}; | ||
export default roleCheckMiddleware; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters