Some checks are pending
Docker Test / test (push) Waiting to run
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.
26 lines
No EOL
659 B
JavaScript
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 }; |