Skip to content

Commit da58e49

Browse files
authored
Merge pull request #38 from adityacosmos24/main
feat: added refresh access token and logout
2 parents 17de288 + 9dd1874 commit da58e49

File tree

10 files changed

+1143
-8
lines changed

10 files changed

+1143
-8
lines changed

API_DOCUMENTATION.md

Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
# 🔐 RBAC API Documentation
2+
3+
## Authentication Endpoints
4+
5+
### POST /api/auth/register
6+
Register a new user in the system.
7+
8+
**Request Body:**
9+
```json
10+
{
11+
"username": "string",
12+
"email": "string",
13+
"fullname": "string",
14+
"password": "string"
15+
}
16+
```
17+
18+
**Response:**
19+
```json
20+
{
21+
"success": true,
22+
"message": "User registered successfully",
23+
"user": {
24+
"id": "user_id",
25+
"username": "username",
26+
"email": "email",
27+
"role": "User"
28+
}
29+
}
30+
```
31+
32+
### POST /api/auth/login
33+
Authenticate user and return access and refresh tokens.
34+
35+
**Request Body:**
36+
```json
37+
{
38+
"email": "string",
39+
"password": "string"
40+
}
41+
```
42+
43+
**Response:**
44+
```json
45+
{
46+
"success": true,
47+
"message": "Login successful",
48+
"accessToken": "jwt_access_token",
49+
"refreshToken": "jwt_refresh_token",
50+
"user": {
51+
"id": "user_id",
52+
"username": "username",
53+
"email": "email",
54+
"fullname": "fullname",
55+
"role": "User"
56+
}
57+
}
58+
```
59+
60+
### POST /api/auth/refresh
61+
Refresh access token using refresh token.
62+
63+
**Request Body:**
64+
```json
65+
{
66+
"refreshToken": "jwt_refresh_token"
67+
}
68+
```
69+
70+
**Response:**
71+
```json
72+
{
73+
"success": true,
74+
"message": "Token refreshed successfully",
75+
"accessToken": "new_jwt_access_token",
76+
"user": {
77+
"id": "user_id",
78+
"username": "username",
79+
"email": "email",
80+
"fullname": "fullname",
81+
"role": "User"
82+
}
83+
}
84+
```
85+
86+
### POST /api/auth/logout
87+
Logout user and invalidate refresh token.
88+
89+
**Request Body:**
90+
```json
91+
{
92+
"refreshToken": "jwt_refresh_token"
93+
}
94+
```
95+
96+
**Response:**
97+
```json
98+
{
99+
"success": true,
100+
"message": "Logged out successfully"
101+
}
102+
```
103+
104+
## Role Management Endpoints
105+
106+
### GET /api/roles
107+
Get all roles (requires authentication).
108+
109+
**Headers:**
110+
```
111+
Authorization: Bearer <access_token>
112+
```
113+
114+
**Response:**
115+
```json
116+
[
117+
{
118+
"_id": "role_id",
119+
"name": "Admin",
120+
"permissions": [
121+
{
122+
"_id": "permission_id",
123+
"name": "Manage Users",
124+
"description": "Admin can manage users"
125+
}
126+
]
127+
}
128+
]
129+
```
130+
131+
### POST /api/roles
132+
Create a new role (requires authentication).
133+
134+
**Headers:**
135+
```
136+
Authorization: Bearer <access_token>
137+
```
138+
139+
**Request Body:**
140+
```json
141+
{
142+
"name": "string",
143+
"permissions": ["permission_id_1", "permission_id_2"]
144+
}
145+
```
146+
147+
## Permission Management Endpoints
148+
149+
### GET /api/permissions
150+
Get all permissions (requires authentication).
151+
152+
**Headers:**
153+
```
154+
Authorization: Bearer <access_token>
155+
```
156+
157+
### POST /api/permissions
158+
Create a new permission (requires authentication).
159+
160+
**Headers:**
161+
```
162+
Authorization: Bearer <access_token>
163+
```
164+
165+
**Request Body:**
166+
```json
167+
{
168+
"name": "string",
169+
"description": "string"
170+
}
171+
```
172+
173+
## RBAC Test Endpoints
174+
175+
### GET /api/rbac-test/admin-only
176+
Test endpoint for Admin role only.
177+
178+
**Headers:**
179+
```
180+
Authorization: Bearer <access_token>
181+
```
182+
183+
**Response:**
184+
```json
185+
{
186+
"message": "Welcome, Admin"
187+
}
188+
```
189+
190+
### GET /api/rbac-test/user-only
191+
Test endpoint for User role only.
192+
193+
**Headers:**
194+
```
195+
Authorization: Bearer <access_token>
196+
```
197+
198+
**Response:**
199+
```json
200+
{
201+
"message": "Welcome, User"
202+
}
203+
```
204+
205+
## Error Responses
206+
207+
All endpoints return consistent error responses:
208+
209+
```json
210+
{
211+
"success": false,
212+
"message": "Error description"
213+
}
214+
```
215+
216+
Common HTTP status codes:
217+
- `400` - Bad Request
218+
- `401` - Unauthorized
219+
- `403` - Forbidden
220+
- `404` - Not Found
221+
- `500` - Internal Server Error
222+
223+
## Security Features
224+
225+
- **JWT Access Tokens**: Short-lived (1 day) for API access
226+
- **Refresh Tokens**: Long-lived (7 days) for token renewal
227+
- **Password Hashing**: bcrypt with salt rounds
228+
- **Role-Based Access Control**: Granular permissions
229+
- **Token Invalidation**: Secure logout mechanism

README.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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,25 @@ 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
6367
PORT=5000
68+
69+
# Database Configuration
6470
MONGO_URI=mongodb://localhost:27017/rbac
65-
JWT_SECRET=your-secret-key
71+
72+
# JWT Configuration
73+
JWT_SECRET=your-super-secret-jwt-key-here
74+
JWT_EXPIRY=1d
75+
76+
# Refresh Token Configuration
77+
REFRESH_TOKEN_SECRET=your-super-secret-refresh-token-key-here
78+
REFRESH_TOKEN_EXPIRY=7d
79+
80+
# CORS Configuration
81+
CORS_URL=http://localhost:3000
6682
```
6783

6884
### 4️⃣ Run the Project
@@ -71,6 +87,76 @@ JWT_SECRET=your-secret-key
7187
npm run dev
7288
```
7389

90+
### 5️⃣ Seed the Database
91+
92+
Before using the application, seed the database with default roles and permissions:
93+
94+
```bash
95+
node src/seed/seedRoles.js
96+
```
97+
98+
---
99+
100+
## 🔌 API Endpoints
101+
102+
### Authentication Endpoints
103+
104+
| Method | Endpoint | Description | Body |
105+
|--------|----------|-------------|------|
106+
| POST | `/api/auth/register` | Register a new user | `{username, email, fullname, password}` |
107+
| POST | `/api/auth/login` | Login user | `{email, password}` |
108+
| POST | `/api/auth/refresh` | Refresh access token | `{refreshToken}` |
109+
| POST | `/api/auth/logout` | Logout user | `{refreshToken}` |
110+
111+
### Role Management Endpoints
112+
113+
| Method | Endpoint | Description | Auth Required |
114+
|--------|----------|-------------|---------------|
115+
| GET | `/api/roles` | Get all roles | Yes |
116+
| POST | `/api/roles` | Create new role | Yes |
117+
| GET | `/api/roles/:id` | Get role by ID | Yes |
118+
| PUT | `/api/roles/:id` | Update role | Yes |
119+
| DELETE | `/api/roles/:id` | Delete role | Yes |
120+
| PUT | `/api/roles/:id/permissions` | Assign permissions to role | Yes |
121+
122+
### Permission Management Endpoints
123+
124+
| Method | Endpoint | Description | Auth Required |
125+
|--------|----------|-------------|---------------|
126+
| GET | `/api/permissions` | Get all permissions | Yes |
127+
| POST | `/api/permissions` | Create new permission | Yes |
128+
| GET | `/api/permissions/:id` | Get permission by ID | Yes |
129+
| PUT | `/api/permissions/:id` | Update permission | Yes |
130+
| DELETE | `/api/permissions/:id` | Delete permission | Yes |
131+
132+
### RBAC Test Endpoints
133+
134+
| Method | Endpoint | Description | Auth Required |
135+
|--------|----------|-------------|---------------|
136+
| GET | `/api/rbac-test/admin-only` | Admin only access | Yes (Admin role) |
137+
| GET | `/api/rbac-test/user-only` | User only access | Yes (User role) |
138+
139+
---
140+
141+
## 🔄 Authentication Flow
142+
143+
### Login Flow
144+
1. User sends credentials to `/api/auth/login`
145+
2. Server validates credentials
146+
3. Server generates both access token (short-lived) and refresh token (long-lived)
147+
4. Both tokens are returned to client
148+
149+
### Token Refresh Flow
150+
1. When access token expires, client sends refresh token to `/api/auth/refresh`
151+
2. Server validates refresh token
152+
3. Server generates new access token
153+
4. New access token is returned to client
154+
155+
### Logout Flow
156+
1. Client sends refresh token to `/api/auth/logout`
157+
2. Server invalidates the refresh token in database
158+
3. Client should discard both tokens
159+
74160
---
75161

76162
### 🔄 System Flows

0 commit comments

Comments
 (0)