Skip to content

Implement the role-based authorization middleware and set up the infrastructure for admin-exclusive routes#42

Merged
ayshadogo merged 1 commit intoDfunder:mainfrom
gabito1451:role-based-authorization-middleware
Mar 5, 2026
Merged

Implement the role-based authorization middleware and set up the infrastructure for admin-exclusive routes#42
ayshadogo merged 1 commit intoDfunder:mainfrom
gabito1451:role-based-authorization-middleware

Conversation

@gabito1451
Copy link
Contributor

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.

@ayshadogo ayshadogo merged commit 99f2f55 into Dfunder:main Mar 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Role-Based Access Control (RBAC) Middleware

2 participants