Repository for OpenSpringFest (OSF)
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 π.
- β 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
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
git clone https://github.com/<your-org>/rbac-auth.git
cd rbac-authnpm install
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.npm run dev
Before using the application, seed the database with default roles and permissions:
node src/seed/seedRoles.js| 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} |
| 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 |
| 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 |
| 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) |
- 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
- 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
- Client sends refresh token to
/api/auth/logout - Server invalidates the refresh token in database
- Client should discard both tokens
π 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
Admin can create roles and assign permissions
Users get assigned roles
Middleware checks role/permission before accessing API
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 π
Keep PRs small & focused
Follow coding style (ESLint + Prettier recommended)
Add tests when introducing new features
Use issues to discuss before large changes
π 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.