Implement the role-based authorization middleware and set up the infrastructure for admin-exclusive routes#42
Merged
ayshadogo merged 1 commit intoDfunder:mainfrom Mar 5, 2026
Conversation
…astructure for admin-exclusive routes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
closes #27
I have implemented the role-based authorization middleware and set up the infrastructure for admin-exclusive routes as requested.
Changes Made:
Created
src/middlewares/authorize.js:
Developed a flexible middleware that accepts an array of allowed roles (e.g., authorize('admin')).
It verifies the req.user object (populated by the authenticate middleware) and checks if the user's role matches any of the permitted roles.
Returns a 403 Forbidden error with an operational flag if access is denied.
Created
src/routes/admin.routes.js :
Established a new router dedicated to administrative tasks.
Applied both authenticate and authorize('admin')
as global middleware for this router to ensure all contained routes are fully protected.
Added sample endpoints for an admin dashboard and user management to demonstrate functionality.
Updated
src/app.js :
Registered the new adminRoutes at the /api/admin path.
Added
src/tests/authorize.test.js :
Implemented unit tests to verify that the middleware correctly allows access for permitted roles and rejects access for unauthorized users or unauthenticated requests.
Confirmed all tests passed using Jest.