helpyourneighbour/backend/middleware/role.middleware.js
BibaBot 901bcf454c
Some checks are pending
Docker Test / test (push) Waiting to run
feat: Add role-based access control tests and fix middleware export
This commit adds comprehensive unit tests for the role-based access control middleware and fixes the ES module export issue. The tests verify that users with correct roles can access protected routes, while users with incorrect roles or no authentication are properly denied access.
2026-03-17 19:07:03 +00:00

26 lines
No EOL
659 B
JavaScript

// Role-based access control middleware
const requireRole = (requiredRoles) => {
return (req, res, next) => {
// Check if user is authenticated
if (!req.user) {
return res.status(401).json({
error: 'Authentication required'
});
}
// Check if user has the required role
const userRole = req.user.role;
if (requiredRoles.includes(userRole)) {
// User has the required role, allow access
next();
} else {
// User does not have the required role, deny access
return res.status(403).json({
error: 'Insufficient permissions'
});
}
};
};
export { requireRole };