cca-auth-module 0.1.87 → 0.1.88

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -8,131 +8,380 @@ This module provides endpoints and methods for user authentication, including lo
8
8
 
9
9
  ## Endpoints
10
10
 
11
- ### 1. **Login**
12
-
13
- - **URL:** `/auth/login`
14
- - **Method:** `POST`
15
- - **Description:** Authenticates a user and returns tokens or authentication data.
16
- - **Body Parameters:**
17
- - `adminPassword` (optional, string): An additional password if admin access is required.
18
- - Other properties as defined in `LoginDTO` (e.g., email, password, etc.).
19
- - **Success Response:**
20
- - **Code:** `201 Created`
21
- - **Content:** JSON object with authentication result (tokens, user details, etc.).
11
+ # Auth API Documentation
22
12
 
23
13
  ---
24
14
 
25
- ### 2. **Logout**
15
+ ## 1. Login
26
16
 
27
- - **URL:** `/auth/logout/:id`
28
- - **Method:** `POST` or `GET` (typically POST)
29
- - **Description:** Logs out the user by invalidating the active session or token.
30
- - **URL Parameters:**
31
- - `id` (string): The identifier for the user/session.
32
- - **Success Response:**
33
- - **Code:** `200 OK`
34
- - **Content:** JSON message confirming successful logout:
35
- ```json
36
- { "message": "Logged out successfully" }
37
- ```
17
+ **POST** `/auth/login`
18
+
19
+ ### Request Body
20
+
21
+ ```json
22
+ {
23
+ "email": "user@example.com",
24
+ "password": "yourPassword"
25
+ }
26
+ ```
27
+
28
+ ### Success Response
29
+
30
+ **Status:** `200 OK`
31
+
32
+ ```json
33
+ {
34
+ "success": true,
35
+ "message": "Login successful",
36
+ "data": {
37
+ "accessToken": "string",
38
+ "userId": "string",
39
+ "expiresAt": "2025-08-06T12:00:00.000Z",
40
+ "auth": {
41
+ "hasAccessToken": true,
42
+ "enable": false,
43
+ "status": "BASIC_AUTH",
44
+ "verified": false
45
+ }
46
+ },
47
+ "meta": {
48
+ "timestamp": "2025-08-06T11:59:59.999Z"
49
+ }
50
+ }
51
+ ```
52
+
53
+ ---
54
+
55
+ ## 2. Admin Login
56
+
57
+ **POST** `/auth/admin-login`
58
+
59
+ ### Request Body
60
+
61
+ ```json
62
+ {
63
+ "email": "admin@example.com",
64
+ "password": "adminPassword",
65
+ "adminPassword": "superSecretAdminPassword"
66
+ }
67
+ ```
68
+
69
+ ### Success Response
70
+
71
+ **Status:** `201 Created`
72
+
73
+ ```json
74
+ {
75
+ "success": true,
76
+ "message": "Admin login successful",
77
+ "data": {
78
+ "message": "Admin authenticated",
79
+ "auth": {
80
+ "hasAccessToken": true,
81
+ "enable": false,
82
+ "status": "BASIC_AUTH",
83
+ "verified": false
84
+ }
85
+ }
86
+ }
87
+ ```
88
+
89
+ ---
90
+
91
+ ## 3. Logout
92
+
93
+ **POST** `/auth/logout`
94
+
95
+ ### Request Body
96
+
97
+ ```json
98
+ {
99
+ "id": "userId"
100
+ }
101
+ ```
102
+
103
+ ### Success Response
104
+
105
+ **Status:** `200 OK`
106
+
107
+ ```json
108
+ {
109
+ "success": true,
110
+ "message": "Logout successful",
111
+ "data": {
112
+ "auth": {
113
+ "hasAccessToken": false,
114
+ "enable": false,
115
+ "status": "LOGGED_OUT",
116
+ "verified": false
117
+ }
118
+ }
119
+ }
120
+ ```
121
+
122
+ ---
123
+
124
+ ## 4. Register
125
+
126
+ **POST** `/auth/register`
127
+
128
+ ### Request Body
129
+
130
+ ```json
131
+ {
132
+ "email": "newuser@example.com",
133
+ "name": "New User",
134
+ "password": "userPassword",
135
+ "role": "user",
136
+ "adminPassword": "optionalAdminPassword"
137
+ }
138
+ ```
139
+
140
+ ### Success Response
141
+
142
+ **Status:** `200 OK`
143
+
144
+ ```json
145
+ {
146
+ "success": true,
147
+ "message": "Registration successful",
148
+ "data": {
149
+ "auth": {
150
+ "hasAccessToken": false,
151
+ "enable": false,
152
+ "status": "REGISTERED",
153
+ "verified": false
154
+ }
155
+ },
156
+ "meta": {
157
+ "status": true
158
+ }
159
+ }
160
+ ```
38
161
 
39
162
  ---
40
163
 
41
- ### 3. **Register**
42
-
43
- - **URL:** `/auth/register`
44
- - **Method:** `POST`
45
- - **Description:** Registers a new user.
46
- - **Body Parameters:**
47
- - `email` (string): The user's email.
48
- - `name` (string): The user's name.
49
- - `password` (string): The user's password.
50
- - `role` (string): The role assigned to the user.
51
- - `adminPassword` (optional, string): Additional admin password if required.
52
- - **Success Response:**
53
- - **Code:** `201 Created`
54
- - **Content:** May return a success message or the created user details.
55
- - **Note:** The registration endpoint does not directly return a JSON response on success in the given implementation. Consider adding a response message if needed.
164
+ ## 5. Refresh Token
165
+
166
+ **POST** `/auth/refresh-token`
167
+
168
+ ### Request Body
169
+
170
+ ```json
171
+ {
172
+ "refreshToken": "yourRefreshToken"
173
+ }
174
+ ```
175
+
176
+ ### Success Response
177
+
178
+ **Status:** `200 OK`
179
+
180
+ ```json
181
+ {
182
+ "success": true,
183
+ "message": "Token refreshed successfully",
184
+ "data": {
185
+ "accessToken": "newAccessToken",
186
+ "refreshToken": "newRefreshToken",
187
+ "auth": {
188
+ "hasAccessToken": true,
189
+ "enable": false,
190
+ "status": "BASIC_AUTH",
191
+ "verified": false
192
+ }
193
+ }
194
+ }
195
+ ```
56
196
 
57
197
  ---
58
198
 
59
- ### 4. **Refresh Token**
199
+ ## 6. 2FA Setup
200
+
201
+ **POST** `/auth/2fa/setup`
60
202
 
61
- - **URL:** `/auth/refresh-token`
62
- - **Method:** `POST`
63
- - **Description:** Generates a new authentication token using a refresh token.
64
- - **Body Parameters:**
65
- - `refreshToken` (string): The refresh token provided to the user.
66
- - **Success Response:**
67
- - **Code:** `200 OK`
68
- - **Content:** JSON object containing the new token(s).
203
+ ### Headers
204
+
205
+ ```
206
+ Authorization: Bearer <accessToken>
207
+ ```
208
+
209
+ ### Request Body
210
+
211
+ None
212
+
213
+ ### Success Response
214
+
215
+ **Status:** `200 OK`
216
+
217
+ ```json
218
+ {
219
+ "success": true,
220
+ "message": "2FA setup successful",
221
+ "data": {
222
+ "qrCode": "data:image/png;base64,...",
223
+ "auth": {
224
+ "hasAccessToken": true,
225
+ "enable": false,
226
+ "status": "NEEDS_SETUP"
227
+ }
228
+ },
229
+ "meta": {
230
+ "nextStep": "Scan the QR code and enter your first code to verify",
231
+ "redirectTo": "/2fa-enable"
232
+ }
233
+ }
234
+ ```
69
235
 
70
236
  ---
71
237
 
72
- ### 5. **Verify Token**
238
+ ## 7. Enable 2FA
239
+
240
+ **POST** `/auth/2fa/enable`
73
241
 
74
- - **Description:** Verifies the validity of a provided token.
75
- - **Usage:** This is a helper method (not an HTTP endpoint) that accepts a token string and returns the decoded token payload.
76
- - **Method Signature:**
77
- ```typescript
78
- verifyToken(token: string): Promise<IDecodedToken>
79
- ```
242
+ ### Headers
243
+
244
+ ```
245
+ Authorization: Bearer <accessToken>
246
+ ```
247
+
248
+ ### Request Body
249
+
250
+ ```json
251
+ {
252
+ "userId": "userId",
253
+ "token": "2faToken"
254
+ }
255
+ ```
256
+
257
+ ### Success Response
258
+
259
+ **Status:** `200 OK`
260
+
261
+ ```json
262
+ {
263
+ "success": true,
264
+ "message": "2FA enabled successfully",
265
+ "data": {
266
+ "isEnabled": true,
267
+ "enabledAt": "2025-08-06T12:00:00.000Z",
268
+ "auth": {
269
+ "hasAccessToken": true,
270
+ "enable": true,
271
+ "status": "PENDING_VERIFICATION"
272
+ }
273
+ },
274
+ "meta": {
275
+ "nextStep": "Proceed to verify with a valid 2FA token",
276
+ "redirectTo": "/verify-2fa"
277
+ }
278
+ }
279
+ ```
80
280
 
81
281
  ---
82
282
 
83
- ## Usage Example
283
+ ## 8. Verify 2FA
84
284
 
85
- Below is an example of integrating the Auth Module into an Express application:
285
+ **POST** `/auth/2fa/verify`
86
286
 
87
- ```typescript
88
- import express from 'express';
89
- import { AuthController, authConfig, createAuthContainer } from 'path-to-your-auth-module';
90
- import { ConfigSource } from 'path-to-your-domain-interfaces';
287
+ ### Request Body
91
288
 
92
- const app = express();
93
- app.use(express.json());
289
+ ```json
290
+ {
291
+ "userId": "userId",
292
+ "token": "2faVerificationToken"
293
+ }
294
+ ```
94
295
 
95
- // Configure auth module with your config source
96
- const configSource: ConfigSource = {
97
- accessTokenSecret: process.env.ACCESS_TOKEN_SECRET || 'default-access-secret',
98
- refreshTokenSecret: process.env.REFRESH_TOKEN_SECRET || 'default-refresh-secret',
99
- accessTokenExpiry: parseInt(process.env.ACCESS_TOKEN_EXPIRY || '3600'),
100
- refreshTokenExpiry: parseInt(process.env.REFRESH_TOKEN_EXPIRY || '86400'),
101
- adminSecretPassword: process.env.ADMIN_SECRET_PASSWORD || 'default-admin-password'
102
- };
296
+ ### Success Response
297
+
298
+ **Status:** `200 OK`
299
+
300
+ ```json
301
+ {
302
+ "success": true,
303
+ "message": "2FA verification successful",
304
+ "data": {
305
+ "token": "accessToken",
306
+ "refreshToken": "refreshToken",
307
+ "user": {
308
+ "id": "userId",
309
+ "email": "user@example.com",
310
+ "name": "User Name",
311
+ "role": "user"
312
+ },
313
+ "auth": {
314
+ "hasAccessToken": true,
315
+ "enable": true,
316
+ "status": "FULL_AUTH",
317
+ "verified": true
318
+ }
319
+ },
320
+ "meta": {
321
+ "recommendation": "You're fully authenticated",
322
+ "redirectTo": "/"
323
+ }
324
+ }
325
+ ```
103
326
 
104
- // Initialize auth configuration
105
- authConfig(configSource);
327
+ ---
328
+
329
+ ## 9. Disable 2FA
106
330
 
107
- // Create a container and resolve the AuthController
108
- const container = createAuthContainer();
109
- const authController: AuthController = container.resolve(AuthController);
331
+ **POST** `/auth/2fa/disable`
110
332
 
111
- // Define routes for authentication
112
- app.post('/auth/login', authController.login);
113
- app.post('/auth/logout/:id', authController.logout);
114
- app.post('/auth/register', authController.register);
115
- app.post('/auth/refresh-token', authController.refreshToken);
333
+ ### Headers
116
334
 
117
- // Error handling middleware
118
- app.use((err, req, res, next) => {
119
- res.status(500).json({ error: err.message });
120
- });
335
+ ```
336
+ Authorization: Bearer <accessToken>
337
+ ```
338
+
339
+ ### Request Body
340
+
341
+ ```json
342
+ {
343
+ "token": "current2faToken"
344
+ }
345
+ ```
121
346
 
122
- app.listen(3000, () => console.log('Server running on port 3000'));
347
+ ### Success Response
348
+
349
+ **Status:** `200 OK`
350
+
351
+ ```json
352
+ {
353
+ "success": true,
354
+ "message": "2FA disabled successfully",
355
+ "data": {
356
+ "disabledAt": "2025-08-06T12:00:00.000Z",
357
+ "auth": {
358
+ "hasAccessToken": true,
359
+ "enable": false,
360
+ "status": "BASIC_AUTH",
361
+ "verified": false
362
+ }
363
+ },
364
+ "meta": {
365
+ "securityNote": "Account now relies only on password. Re-enable 2FA for better security.",
366
+ "redirectTo": "/login"
367
+ }
368
+ }
123
369
  ```
124
370
 
125
371
  ---
126
372
 
127
- ## Error Handling
373
+ ## Notes
128
374
 
129
- The controller methods throw custom errors such as `ValidationError` and `NotFoundError` when necessary. Ensure your Express application includes error-handling middleware to properly handle these exceptions:
375
+ - All endpoints returning tokens require secure HTTPS connections.
376
+ - 2FA setup, enable, verify, and disable require authentication with a valid access token (sent in the Authorization header).
377
+ - Request bodies use DTOs as defined in the controller (LoginDTO, RegisterDTO, ITwoFactorEnable, ITwoFactorVerify, etc.).
378
+ - Error responses will follow the format:
130
379
 
131
- ```typescript
132
- app.use((err, req, res, next) => {
133
- // Optionally log the error here
134
- res.status(500).json({ error: err.message });
135
- });
380
+ ```json
381
+ {
382
+ "success": false,
383
+ "error": "Error message"
384
+ }
136
385
  ```
137
386
 
138
387
  ---
package/dist/index.js CHANGED
@@ -770,7 +770,6 @@ var _AuthController = class _AuthController {
770
770
  accessToken: result.accessToken,
771
771
  userId: result.id,
772
772
  expiresAt: result.expiresAt,
773
- enabled: twoFactorEnabled,
774
773
  auth: this.createAuthData(
775
774
  true,
776
775
  // hasAccessToken