@@ -12,6 +12,7 @@ This project is developed and maintained under **Opcode, IIIT Bhagalpur** 🚀.
1212## 🌟 Features
1313
1414- ✅ User authentication with ** JWT**
15+ - ✅ ** Refresh Token mechanism** for persistent login
1516- ✅ Secure password hashing (** bcrypt** )
1617- ✅ Role-based access (Admin, User, Moderator, etc.)
1718- ✅ Permission-based middleware for fine-grained access
@@ -59,10 +60,19 @@ npm install
5960
6061### 3️⃣ Setup Environment
6162
62- ```
63+ Create a ` .env ` file in the root directory with the following variables:
64+
65+ ``` env
66+ # Server Configuration
6367PORT=5000
68+
69+ # Database Configuration
6470MONGO_URI=mongodb://localhost:27017/rbac
6571JWT_SECRET=your-secret-key
72+ RESEND_API_KEY=your-resend-api-key
73+
74+ 🔑 Note: The RESEND_API_KEY can be obtained by creating an account on Resend Mail
75+ and generating an API key.
6676```
6777
6878### 4️⃣ Run the Project
@@ -71,6 +81,76 @@ JWT_SECRET=your-secret-key
7181npm run dev
7282```
7383
84+ ### 5️⃣ Seed the Database
85+
86+ Before using the application, seed the database with default roles and permissions:
87+
88+ ``` bash
89+ node src/seed/seedRoles.js
90+ ```
91+
92+ ---
93+
94+ ## 🔌 API Endpoints
95+
96+ ### Authentication Endpoints
97+
98+ | Method | Endpoint | Description | Body |
99+ | --------| ----------| -------------| ------|
100+ | POST | ` /api/auth/register ` | Register a new user | ` {username, email, fullname, password} ` |
101+ | POST | ` /api/auth/login ` | Login user | ` {email, password} ` |
102+ | POST | ` /api/auth/refresh ` | Refresh access token | ` {refreshToken} ` |
103+ | POST | ` /api/auth/logout ` | Logout user | ` {refreshToken} ` |
104+
105+ ### Role Management Endpoints
106+
107+ | Method | Endpoint | Description | Auth Required |
108+ | --------| ----------| -------------| ---------------|
109+ | GET | ` /api/roles ` | Get all roles | Yes |
110+ | POST | ` /api/roles ` | Create new role | Yes |
111+ | GET | ` /api/roles/:id ` | Get role by ID | Yes |
112+ | PUT | ` /api/roles/:id ` | Update role | Yes |
113+ | DELETE | ` /api/roles/:id ` | Delete role | Yes |
114+ | PUT | ` /api/roles/:id/permissions ` | Assign permissions to role | Yes |
115+
116+ ### Permission Management Endpoints
117+
118+ | Method | Endpoint | Description | Auth Required |
119+ | --------| ----------| -------------| ---------------|
120+ | GET | ` /api/permissions ` | Get all permissions | Yes |
121+ | POST | ` /api/permissions ` | Create new permission | Yes |
122+ | GET | ` /api/permissions/:id ` | Get permission by ID | Yes |
123+ | PUT | ` /api/permissions/:id ` | Update permission | Yes |
124+ | DELETE | ` /api/permissions/:id ` | Delete permission | Yes |
125+
126+ ### RBAC Test Endpoints
127+
128+ | Method | Endpoint | Description | Auth Required |
129+ | --------| ----------| -------------| ---------------|
130+ | GET | ` /api/rbac-test/admin-only ` | Admin only access | Yes (Admin role) |
131+ | GET | ` /api/rbac-test/user-only ` | User only access | Yes (User role) |
132+
133+ ---
134+
135+ ## 🔄 Authentication Flow
136+
137+ ### Login Flow
138+ 1 . User sends credentials to ` /api/auth/login `
139+ 2 . Server validates credentials
140+ 3 . Server generates both access token (short-lived) and refresh token (long-lived)
141+ 4 . Both tokens are returned to client
142+
143+ ### Token Refresh Flow
144+ 1 . When access token expires, client sends refresh token to ` /api/auth/refresh `
145+ 2 . Server validates refresh token
146+ 3 . Server generates new access token
147+ 4 . New access token is returned to client
148+
149+ ### Logout Flow
150+ 1 . Client sends refresh token to ` /api/auth/logout `
151+ 2 . Server invalidates the refresh token in database
152+ 3 . Client should discard both tokens
153+
74154---
75155
76156### 🔄 System Flows
0 commit comments