helpyourneighbour/backend/middleware/role.middleware.js
BibaBot Jarvis 30bd7f0214
Some checks are pending
Docker Test / test (push) Waiting to run
feat: Implement role-based access control middleware and update documentation
2026-03-16 03:06:38 +00:00

20 lines
No EOL
594 B
JavaScript

/**
* 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();
};
};