RBAC (Role Based Access Control)
Repository for OpenSpringFest (OSF)
π RBAC Authentication System
A secure and extendable Role-Based Access Control (RBAC) authentication system built with Node.js, Express, and MongoDB.
This project is developed and maintained under Opcode, IIIT Bhagalpur π.
π Features
- β
User authentication with JWT
- β
Refresh Token mechanism for persistent login
- β
Secure password hashing (bcrypt)
- β
Role-based access (Admin, User, Moderator, etc.)
- β
Permission-based middleware for fine-grained access
- β
Modular project structure for scalability
- β
Ready for extension & contribution by the community
π Project Structure
rbac-auth/
βββ src/
β βββ config/ # DB & environment configs
β βββ controllers/ # Request handlers
β βββ middlewares/ # Auth & RBAC middlewares
β βββ models/ # MongoDB schemas (User, Role, Permission)
β βββ routes/ # API routes
β βββ services/ # Business logic (auth, role mgmt)
β βββ utils/ # Helper functions
β βββ index.js # Entry point
βββ tests/ # Unit & integration tests
βββ .env.example # Sample environment variables
βββ .gitignore
βββ package.json
βββ README.md
π Getting Started
1οΈβ£ Clone the Repository
git clone https://github.com/<your-org>/rbac-auth.git
cd rbac-auth
2οΈβ£ Install Dependencies
npm install
3οΈβ£ Setup Environment
Create a .env file in the root directory with the following variables:
# Server Configuration
PORT=5000
# Database Configuration
MONGO_URI=mongodb://localhost:27017/rbac
JWT_SECRET=your-secret-key
RESEND_API_KEY=your-resend-api-key
π Note: The RESEND_API_KEY can be obtained by creating an account on Resend Mail
and generating an API key.
4οΈβ£ Run the Project
npm run dev
5οΈβ£ Seed the Database
Before using the application, seed the database with default roles and permissions:
node src/seed/seedRoles.js
π API Endpoints
Authentication Endpoints
| Method |
Endpoint |
Description |
Body |
| POST |
/api/auth/register |
Register a new user |
{username, email, fullname, password} |
| POST |
/api/auth/login |
Login user |
{email, password} |
| POST |
/api/auth/refresh |
Refresh access token |
{refreshToken} |
| POST |
/api/auth/logout |
Logout user |
{refreshToken} |
Role Management Endpoints
| Method |
Endpoint |
Description |
Auth Required |
| GET |
/api/roles |
Get all roles |
Yes |
| POST |
/api/roles |
Create new role |
Yes |
| GET |
/api/roles/:id |
Get role by ID |
Yes |
| PUT |
/api/roles/:id |
Update role |
Yes |
| DELETE |
/api/roles/:id |
Delete role |
Yes |
| PUT |
/api/roles/:id/permissions |
Assign permissions to role |
Yes |
Permission Management Endpoints
| Method |
Endpoint |
Description |
Auth Required |
| GET |
/api/permissions |
Get all permissions |
Yes |
| POST |
/api/permissions |
Create new permission |
Yes |
| GET |
/api/permissions/:id |
Get permission by ID |
Yes |
| PUT |
/api/permissions/:id |
Update permission |
Yes |
| DELETE |
/api/permissions/:id |
Delete permission |
Yes |
RBAC Test Endpoints
| Method |
Endpoint |
Description |
Auth Required |
| GET |
/api/rbac-test/admin-only |
Admin only access |
Yes (Admin role) |
| GET |
/api/rbac-test/user-only |
User only access |
Yes (User role) |
π Authentication Flow
Login Flow
- User sends credentials to
/api/auth/login
- Server validates credentials
- Server generates both access token (short-lived) and refresh token (long-lived)
- Both tokens are returned to client
Token Refresh Flow
- When access token expires, client sends refresh token to
/api/auth/refresh
- Server validates refresh token
- Server generates new access token
- New access token is returned to client
Logout Flow
- Client sends refresh token to
/api/auth/logout
- Server invalidates the refresh token in database
- Client should discard both tokens
π³ Docker Deployment
This application can be easily deployed using Docker! See README.Docker.md for detailed instructions.
Quick Start with Docker Compose
# Copy environment variables
cp .env.example .env
# Start the application (production mode)
docker compose up -d
# Start in development mode with hot reload
docker compose --profile dev up -d app-dev
# View logs
docker compose logs -f
# Stop services
docker compose down
Note: Use docker compose (v2) or docker-compose (v1) depending on your installation.
The Docker setup includes:
- β
Optimized multi-stage Dockerfile
- β
MongoDB database container
- β
Non-root user for security
- β
Health checks for services
- β
Development and production configurations
- β
Volume persistence for database
π System Flows
π Authentication Flow
User signs up β password hashed β stored in DB
User logs in β JWT issued
JWT validated for protected routes
RBAC middleware checks user role/permission
π Role & Permission Flow
Admin can create roles and assign permissions
Users get assigned roles
Middleware checks role/permission before accessing API
π Contribution Guide
We β€οΈ contributions! Follow these steps to get started:
Fork the repository
Create a branch (feature/auth-flow, fix/bug-x)
Commit changes (use clear, descriptive messages)
Push your branch
Open a Pull Request π
β
Contribution Hints
Keep PRs small & focused
Follow coding style (ESLint + Prettier recommended)
Add tests when introducing new features
Use issues to discuss before large changes
π Hints for Contributors
π Always hash passwords before storing
π Use JWT for stateless authentication
π Centralize RBAC logic in middlewares
π§© Keep business logic in services, not controllers
π Write meaningful commit messages
π€ Community
This project is part of Opcode, IIIT Bhagalpur.
Maintainers will review PRs, suggest changes, and merge contributions.
Use Issues to report bugs or suggest features.
π License
This project is licensed under the MIT License.
Youβre free to use, modify, and distribute this project with attribution.