feat: implement role-based access control and auth routes
Some checks are pending
Docker Test / test (push) Waiting to run
Some checks are pending
Docker Test / test (push) Waiting to run
This commit implements the role-based access control middleware and authentication routes as per the project's requirements. It includes:
This commit is contained in:
parent
437bb1d504
commit
e278ee3da5
4 changed files with 274 additions and 0 deletions
20
backend/middleware/requireRole.js
Normal file
20
backend/middleware/requireRole.js
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/**
|
||||
* Middleware to require a specific role for an endpoint.
|
||||
* @param {string[]} allowedRoles - Array of roles allowed to access the endpoint.
|
||||
* @returns {function} Express middleware function.
|
||||
*/
|
||||
module.exports = (allowedRoles) => {
|
||||
return (req, res, next) => {
|
||||
const userRole = req.user?.role;
|
||||
|
||||
if (!userRole) {
|
||||
return res.status(401).json({ error: 'Authorization required' });
|
||||
}
|
||||
|
||||
if (!allowedRoles.includes(userRole)) {
|
||||
return res.status(403).json({ error: 'Insufficient permissions' });
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue