feat: Implement role-based access control middleware and update documentation
Some checks are pending
Docker Test / test (push) Waiting to run
Some checks are pending
Docker Test / test (push) Waiting to run
This commit is contained in:
parent
7c9862a08a
commit
30bd7f0214
4 changed files with 65 additions and 2 deletions
20
backend/middleware/role.middleware.js
Normal file
20
backend/middleware/role.middleware.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* Middleware to check if the user has the required role(s)
|
||||
* @param {string[]} allowedRoles - Array of roles allowed to access the endpoint
|
||||
* @returns {function} Express middleware function
|
||||
*/
|
||||
export const requireRole = (allowedRoles) => {
|
||||
return (req, res, next) => {
|
||||
const userRole = req.user?.role;
|
||||
|
||||
if (!userRole) {
|
||||
return res.status(401).json({ error: 'Unauthorized: Missing role claim' });
|
||||
}
|
||||
|
||||
if (!allowedRoles.includes(userRole)) {
|
||||
return res.status(403).json({ error: 'Forbidden: Insufficient permissions' });
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue