mbkauthe 2.5.0 → 3.1.0
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/LICENSE +339 -373
- package/README.md +142 -279
- package/docs/api.md +210 -82
- package/docs/db.md +1 -1
- package/docs/error-messages.md +557 -0
- package/index.d.ts +248 -0
- package/index.js +43 -32
- package/lib/config/cookies.js +52 -0
- package/lib/{config.js → config/index.js} +15 -95
- package/lib/config/security.js +8 -0
- package/lib/{pool.js → database/pool.js} +1 -1
- package/lib/main.js +38 -1038
- package/lib/{validateSessionAndRole.js → middleware/auth.js} +5 -3
- package/lib/middleware/index.js +106 -0
- package/lib/routes/auth.js +521 -0
- package/lib/routes/misc.js +263 -0
- package/lib/routes/oauth.js +325 -0
- package/lib/utils/errors.js +257 -0
- package/lib/utils/response.js +21 -0
- package/package.json +21 -11
- package/public/main.js +4 -4
- package/test.spec.js +178 -0
- package/views/Error/dError.handlebars +1 -1
- package/views/errorCodes.handlebars +341 -0
- package/views/showmessage.handlebars +7 -6
|
@@ -0,0 +1,557 @@
|
|
|
1
|
+
# Error Messages & Codes
|
|
2
|
+
|
|
3
|
+
MBKAuthe provides a comprehensive error messaging system with standardized error codes and user-friendly messages.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
The error messaging system provides:
|
|
8
|
+
- **Standardized Error Codes**: Consistent error codes across all operations
|
|
9
|
+
- **User-Friendly Messages**: Clear, actionable error messages for end users
|
|
10
|
+
- **Developer Context**: Detailed error information for logging and debugging
|
|
11
|
+
- **Helpful Hints**: Guidance on how to resolve errors
|
|
12
|
+
- **Structured Responses**: Consistent error response format
|
|
13
|
+
- **Dynamic Documentation**: Error code page at `/mbkauthe/ErrorCode` auto-generates from `lib/utils/errors.js`
|
|
14
|
+
|
|
15
|
+
## Dynamic Error Documentation
|
|
16
|
+
|
|
17
|
+
The error code documentation page (`/mbkauthe/ErrorCode`) is **automatically generated** from the `lib/utils/errors.js` file. This ensures the documentation is always in sync with the actual error codes in the system.
|
|
18
|
+
|
|
19
|
+
### Adding New Errors
|
|
20
|
+
|
|
21
|
+
To add a new error code:
|
|
22
|
+
|
|
23
|
+
1. **Define the error code** in `ErrorCodes` enum:
|
|
24
|
+
```javascript
|
|
25
|
+
export const ErrorCodes = {
|
|
26
|
+
// ... existing codes
|
|
27
|
+
YOUR_NEW_ERROR: 1400,
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
2. **Add error details** to `ErrorMessages` object:
|
|
32
|
+
```javascript
|
|
33
|
+
export const ErrorMessages = {
|
|
34
|
+
// ... existing messages
|
|
35
|
+
[ErrorCodes.YOUR_NEW_ERROR]: {
|
|
36
|
+
message: "Internal message for logging",
|
|
37
|
+
userMessage: "User-friendly error message",
|
|
38
|
+
hint: "Helpful tip to resolve the issue"
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
3. **Include in category array** in `misc.js` route handler:
|
|
44
|
+
```javascript
|
|
45
|
+
const categories = [
|
|
46
|
+
// ... existing categories
|
|
47
|
+
{
|
|
48
|
+
name: "Your Category Name",
|
|
49
|
+
range: "1400-1499",
|
|
50
|
+
codes: [1400, 1401, 1402] // Add your new codes here
|
|
51
|
+
}
|
|
52
|
+
];
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The error code page will automatically display your new error without any additional changes.
|
|
56
|
+
|
|
57
|
+
## Error Code Ranges
|
|
58
|
+
|
|
59
|
+
| Range | Category | Example |
|
|
60
|
+
|-------|----------|---------|
|
|
61
|
+
| 600-699 | Authentication | Invalid credentials, account inactive |
|
|
62
|
+
| 700-799 | Two-Factor Authentication | Invalid 2FA token, 2FA not configured |
|
|
63
|
+
| 800-899 | Session Management | Session expired, invalid session |
|
|
64
|
+
| 900-999 | Authorization | Insufficient permissions, role not allowed |
|
|
65
|
+
| 1000-1099 | Input Validation | Missing fields, invalid format |
|
|
66
|
+
| 1100-1199 | Rate Limiting | Too many requests |
|
|
67
|
+
| 1200-1299 | Server Errors | Internal error, database error |
|
|
68
|
+
| 1300-1399 | OAuth | GitHub not linked, OAuth failed |
|
|
69
|
+
|
|
70
|
+
## Error Codes Reference
|
|
71
|
+
|
|
72
|
+
### Authentication Errors (600-699)
|
|
73
|
+
|
|
74
|
+
#### `601 - INVALID_CREDENTIALS`
|
|
75
|
+
General authentication failure.
|
|
76
|
+
```javascript
|
|
77
|
+
{
|
|
78
|
+
errorCode: 601,
|
|
79
|
+
message: "The username or password you entered is incorrect. Please try again.",
|
|
80
|
+
hint: "Check your spelling and make sure Caps Lock is off"
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### `602 - USER_NOT_FOUND`
|
|
85
|
+
User account doesn't exist.
|
|
86
|
+
```javascript
|
|
87
|
+
{
|
|
88
|
+
errorCode: 602,
|
|
89
|
+
message: "We couldn't find an account with that username.",
|
|
90
|
+
hint: "Please check the username and try again"
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
#### `603 - INCORRECT_PASSWORD`
|
|
95
|
+
Password doesn't match.
|
|
96
|
+
```javascript
|
|
97
|
+
{
|
|
98
|
+
errorCode: 603,
|
|
99
|
+
message: "The password you entered is incorrect.",
|
|
100
|
+
hint: "Make sure you're using the correct password for this account"
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
#### `604 - ACCOUNT_INACTIVE`
|
|
105
|
+
User account is deactivated.
|
|
106
|
+
```javascript
|
|
107
|
+
{
|
|
108
|
+
errorCode: 604,
|
|
109
|
+
message: "Your account has been deactivated.",
|
|
110
|
+
hint: "Please contact your administrator to reactivate your account"
|
|
111
|
+
}
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### `605 - APP_NOT_AUTHORIZED`
|
|
115
|
+
User not authorized for this application.
|
|
116
|
+
```javascript
|
|
117
|
+
{
|
|
118
|
+
errorCode: 605,
|
|
119
|
+
message: "You don't have permission to access this application.",
|
|
120
|
+
hint: "Contact your administrator if you believe this is an error"
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### 2FA Errors (700-799)
|
|
125
|
+
|
|
126
|
+
#### `701 - TWO_FA_REQUIRED`
|
|
127
|
+
2FA verification needed.
|
|
128
|
+
|
|
129
|
+
#### `702 - TWO_FA_INVALID_TOKEN`
|
|
130
|
+
Invalid 2FA code.
|
|
131
|
+
|
|
132
|
+
#### `703 - TWO_FA_NOT_CONFIGURED`
|
|
133
|
+
2FA not set up.
|
|
134
|
+
|
|
135
|
+
#### `704 - TWO_FA_EXPIRED`
|
|
136
|
+
2FA code has expired.
|
|
137
|
+
|
|
138
|
+
### Session Errors (800-899)
|
|
139
|
+
|
|
140
|
+
#### `801 - SESSION_EXPIRED`
|
|
141
|
+
Session has expired.
|
|
142
|
+
|
|
143
|
+
#### `802 - SESSION_INVALID`
|
|
144
|
+
Session is no longer valid.
|
|
145
|
+
|
|
146
|
+
#### `803 - SESSION_NOT_FOUND`
|
|
147
|
+
No active session found.
|
|
148
|
+
|
|
149
|
+
### Authorization Errors (900-999)
|
|
150
|
+
|
|
151
|
+
#### `901 - INSUFFICIENT_PERMISSIONS`
|
|
152
|
+
User lacks required permissions.
|
|
153
|
+
|
|
154
|
+
#### `902 - ROLE_NOT_ALLOWED`
|
|
155
|
+
User's role doesn't have access.
|
|
156
|
+
|
|
157
|
+
### Input Validation Errors (1000-1099)
|
|
158
|
+
|
|
159
|
+
#### `1001 - MISSING_REQUIRED_FIELD`
|
|
160
|
+
Required field not provided.
|
|
161
|
+
|
|
162
|
+
#### `1002 - INVALID_USERNAME_FORMAT`
|
|
163
|
+
Username format is invalid.
|
|
164
|
+
|
|
165
|
+
#### `1003 - INVALID_PASSWORD_LENGTH`
|
|
166
|
+
Password doesn't meet length requirements.
|
|
167
|
+
|
|
168
|
+
#### `1004 - INVALID_TOKEN_FORMAT`
|
|
169
|
+
Token format is incorrect.
|
|
170
|
+
|
|
171
|
+
### Rate Limiting (1100-1199)
|
|
172
|
+
|
|
173
|
+
#### `1101 - RATE_LIMIT_EXCEEDED`
|
|
174
|
+
Too many requests in time window.
|
|
175
|
+
|
|
176
|
+
### Server Errors (1200-1299)
|
|
177
|
+
|
|
178
|
+
#### `1201 - INTERNAL_SERVER_ERROR`
|
|
179
|
+
General server error.
|
|
180
|
+
|
|
181
|
+
#### `1202 - DATABASE_ERROR`
|
|
182
|
+
Database operation failed.
|
|
183
|
+
|
|
184
|
+
#### `1203 - CONFIGURATION_ERROR`
|
|
185
|
+
System configuration issue.
|
|
186
|
+
|
|
187
|
+
### OAuth Errors (1300-1399)
|
|
188
|
+
|
|
189
|
+
#### `1301 - GITHUB_NOT_LINKED`
|
|
190
|
+
GitHub account not linked.
|
|
191
|
+
|
|
192
|
+
#### `1302 - GITHUB_AUTH_FAILED`
|
|
193
|
+
GitHub authentication failed.
|
|
194
|
+
|
|
195
|
+
#### `1303 - OAUTH_STATE_MISMATCH`
|
|
196
|
+
OAuth state verification failed.
|
|
197
|
+
|
|
198
|
+
## API Usage
|
|
199
|
+
|
|
200
|
+
### Import Error Utilities
|
|
201
|
+
|
|
202
|
+
```javascript
|
|
203
|
+
import {
|
|
204
|
+
ErrorCodes,
|
|
205
|
+
ErrorMessages,
|
|
206
|
+
getErrorByCode,
|
|
207
|
+
createErrorResponse,
|
|
208
|
+
logError
|
|
209
|
+
} from 'mbkauthe';
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
### Get Error Details
|
|
213
|
+
|
|
214
|
+
```javascript
|
|
215
|
+
import { getErrorByCode, ErrorCodes } from 'mbkauthe';
|
|
216
|
+
|
|
217
|
+
const error = getErrorByCode(ErrorCodes.INVALID_CREDENTIALS);
|
|
218
|
+
|
|
219
|
+
console.log(error);
|
|
220
|
+
// {
|
|
221
|
+
// errorCode: 601,
|
|
222
|
+
// message: "Invalid username or password",
|
|
223
|
+
// userMessage: "The username or password you entered is incorrect...",
|
|
224
|
+
// hint: "Check your spelling and make sure Caps Lock is off"
|
|
225
|
+
// }
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
### Create Error Response
|
|
229
|
+
|
|
230
|
+
```javascript
|
|
231
|
+
import { createErrorResponse, ErrorCodes } from 'mbkauthe';
|
|
232
|
+
|
|
233
|
+
app.post('/login', async (req, res) => {
|
|
234
|
+
// ... authentication logic
|
|
235
|
+
|
|
236
|
+
if (!authenticated) {
|
|
237
|
+
return res.status(401).json(
|
|
238
|
+
createErrorResponse(401, ErrorCodes.INVALID_CREDENTIALS)
|
|
239
|
+
);
|
|
240
|
+
}
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
// Response:
|
|
244
|
+
// {
|
|
245
|
+
// success: false,
|
|
246
|
+
// statusCode: 401,
|
|
247
|
+
// errorCode: 601,
|
|
248
|
+
// message: "The username or password you entered is incorrect...",
|
|
249
|
+
// hint: "Check your spelling and make sure Caps Lock is off",
|
|
250
|
+
// timestamp: "2025-12-03T12:00:00.000Z"
|
|
251
|
+
// }
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
### Add Custom Data
|
|
255
|
+
|
|
256
|
+
```javascript
|
|
257
|
+
import { createErrorResponse, ErrorCodes } from 'mbkauthe';
|
|
258
|
+
|
|
259
|
+
return res.status(403).json(
|
|
260
|
+
createErrorResponse(403, ErrorCodes.APP_NOT_AUTHORIZED, {
|
|
261
|
+
appName: 'Admin Panel',
|
|
262
|
+
requiredRole: 'SuperAdmin'
|
|
263
|
+
})
|
|
264
|
+
);
|
|
265
|
+
|
|
266
|
+
// Response includes custom data:
|
|
267
|
+
// {
|
|
268
|
+
// success: false,
|
|
269
|
+
// statusCode: 403,
|
|
270
|
+
// errorCode: 605,
|
|
271
|
+
// message: "You don't have permission to access this application.",
|
|
272
|
+
// hint: "Contact your administrator if you believe this is an error",
|
|
273
|
+
// timestamp: "2025-12-03T12:00:00.000Z",
|
|
274
|
+
// appName: "Admin Panel",
|
|
275
|
+
// requiredRole: "SuperAdmin"
|
|
276
|
+
// }
|
|
277
|
+
```
|
|
278
|
+
|
|
279
|
+
### Log Errors
|
|
280
|
+
|
|
281
|
+
```javascript
|
|
282
|
+
import { logError, ErrorCodes } from 'mbkauthe';
|
|
283
|
+
|
|
284
|
+
// Log error with context
|
|
285
|
+
logError('Login attempt', ErrorCodes.INVALID_CREDENTIALS, {
|
|
286
|
+
username: 'john.doe',
|
|
287
|
+
ip: req.ip,
|
|
288
|
+
userAgent: req.headers['user-agent']
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
// Console output:
|
|
292
|
+
// [mbkauthe] Login attempt: {
|
|
293
|
+
// errorCode: 601,
|
|
294
|
+
// message: 'Invalid username or password',
|
|
295
|
+
// username: 'john.doe',
|
|
296
|
+
// ip: '192.168.1.1',
|
|
297
|
+
// userAgent: 'Mozilla/5.0...',
|
|
298
|
+
// timestamp: '2025-12-03T12:00:00.000Z'
|
|
299
|
+
// }
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
## Error Response Format
|
|
303
|
+
|
|
304
|
+
All error responses follow this structure:
|
|
305
|
+
|
|
306
|
+
```typescript
|
|
307
|
+
{
|
|
308
|
+
success: false,
|
|
309
|
+
statusCode: number,
|
|
310
|
+
errorCode: number,
|
|
311
|
+
message: string, // User-friendly message
|
|
312
|
+
hint: string, // How to resolve the error
|
|
313
|
+
timestamp: string, // ISO 8601 timestamp
|
|
314
|
+
...customFields // Optional custom data
|
|
315
|
+
}
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
## Client-Side Error Handling
|
|
319
|
+
|
|
320
|
+
### JavaScript Example
|
|
321
|
+
|
|
322
|
+
```javascript
|
|
323
|
+
async function login(username, password) {
|
|
324
|
+
try {
|
|
325
|
+
const response = await fetch('/mbkauthe/api/login', {
|
|
326
|
+
method: 'POST',
|
|
327
|
+
headers: { 'Content-Type': 'application/json' },
|
|
328
|
+
body: JSON.stringify({ username, password })
|
|
329
|
+
});
|
|
330
|
+
|
|
331
|
+
const data = await response.json();
|
|
332
|
+
|
|
333
|
+
if (!response.ok) {
|
|
334
|
+
// Handle error based on error code
|
|
335
|
+
switch (data.errorCode) {
|
|
336
|
+
case 601: // INVALID_CREDENTIALS
|
|
337
|
+
case 602: // USER_NOT_FOUND
|
|
338
|
+
case 603: // INCORRECT_PASSWORD
|
|
339
|
+
showError('Invalid username or password', data.hint);
|
|
340
|
+
break;
|
|
341
|
+
|
|
342
|
+
case 604: // ACCOUNT_INACTIVE
|
|
343
|
+
showError('Account deactivated', data.hint);
|
|
344
|
+
break;
|
|
345
|
+
|
|
346
|
+
case 605: // APP_NOT_AUTHORIZED
|
|
347
|
+
showError('Access denied', data.hint);
|
|
348
|
+
redirectToHome();
|
|
349
|
+
break;
|
|
350
|
+
|
|
351
|
+
case 1003: // INVALID_PASSWORD_LENGTH
|
|
352
|
+
showFieldError('password', data.message, data.hint);
|
|
353
|
+
break;
|
|
354
|
+
|
|
355
|
+
default:
|
|
356
|
+
showError(data.message, data.hint);
|
|
357
|
+
}
|
|
358
|
+
return;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Handle successful login
|
|
362
|
+
if (data.twoFactorRequired) {
|
|
363
|
+
redirectTo2FA();
|
|
364
|
+
} else {
|
|
365
|
+
redirectToDashboard();
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
} catch (error) {
|
|
369
|
+
showError('Network error', 'Please check your connection and try again');
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function showError(message, hint) {
|
|
374
|
+
const errorDiv = document.getElementById('error-message');
|
|
375
|
+
errorDiv.innerHTML = `
|
|
376
|
+
<div class="error">
|
|
377
|
+
<strong>${message}</strong>
|
|
378
|
+
<p>${hint}</p>
|
|
379
|
+
</div>
|
|
380
|
+
`;
|
|
381
|
+
}
|
|
382
|
+
```
|
|
383
|
+
|
|
384
|
+
### React Example
|
|
385
|
+
|
|
386
|
+
```jsx
|
|
387
|
+
import { ErrorCodes } from 'mbkauthe';
|
|
388
|
+
|
|
389
|
+
function LoginForm() {
|
|
390
|
+
const [error, setError] = useState(null);
|
|
391
|
+
|
|
392
|
+
const handleLogin = async (e) => {
|
|
393
|
+
e.preventDefault();
|
|
394
|
+
|
|
395
|
+
try {
|
|
396
|
+
const response = await fetch('/mbkauthe/api/login', {
|
|
397
|
+
method: 'POST',
|
|
398
|
+
headers: { 'Content-Type': 'application/json' },
|
|
399
|
+
body: JSON.stringify({ username, password })
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
const data = await response.json();
|
|
403
|
+
|
|
404
|
+
if (!response.ok) {
|
|
405
|
+
setError({
|
|
406
|
+
code: data.errorCode,
|
|
407
|
+
message: data.message,
|
|
408
|
+
hint: data.hint
|
|
409
|
+
});
|
|
410
|
+
return;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Success handling...
|
|
414
|
+
|
|
415
|
+
} catch (err) {
|
|
416
|
+
setError({
|
|
417
|
+
message: 'Network error',
|
|
418
|
+
hint: 'Please check your connection'
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
return (
|
|
424
|
+
<form onSubmit={handleLogin}>
|
|
425
|
+
{error && (
|
|
426
|
+
<div className="alert alert-danger">
|
|
427
|
+
<strong>{error.message}</strong>
|
|
428
|
+
{error.hint && <p className="hint">{error.hint}</p>}
|
|
429
|
+
</div>
|
|
430
|
+
)}
|
|
431
|
+
{/* Form fields... */}
|
|
432
|
+
</form>
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
```
|
|
436
|
+
|
|
437
|
+
## Custom Error Messages
|
|
438
|
+
|
|
439
|
+
You can create custom error responses while maintaining the standard format:
|
|
440
|
+
|
|
441
|
+
```javascript
|
|
442
|
+
import { createErrorResponse } from 'mbkauthe';
|
|
443
|
+
|
|
444
|
+
app.post('/custom-action', validateSession, async (req, res) => {
|
|
445
|
+
try {
|
|
446
|
+
// Your logic...
|
|
447
|
+
|
|
448
|
+
if (someCondition) {
|
|
449
|
+
return res.status(400).json(
|
|
450
|
+
createErrorResponse(400, 9999, {
|
|
451
|
+
message: "Custom error message",
|
|
452
|
+
hint: "How to fix this issue",
|
|
453
|
+
additionalData: "Extra context"
|
|
454
|
+
})
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
} catch (error) {
|
|
459
|
+
return res.status(500).json(
|
|
460
|
+
createErrorResponse(500, 1201)
|
|
461
|
+
);
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
```
|
|
465
|
+
|
|
466
|
+
## Best Practices
|
|
467
|
+
|
|
468
|
+
1. **Use Error Codes**: Always use predefined error codes for consistency
|
|
469
|
+
2. **Log Errors**: Use `logError()` for server-side logging
|
|
470
|
+
3. **User-Friendly Messages**: Display `message` to users, not raw error codes
|
|
471
|
+
4. **Provide Hints**: Show `hint` to guide users on resolution
|
|
472
|
+
5. **Don't Expose Internals**: Avoid exposing system details in production
|
|
473
|
+
6. **Handle All Cases**: Have fallback error handling for unexpected errors
|
|
474
|
+
7. **Client-Side Validation**: Validate input before sending to prevent unnecessary errors
|
|
475
|
+
8. **Structured Logging**: Include context (username, IP, etc.) in logs
|
|
476
|
+
|
|
477
|
+
## Security Considerations
|
|
478
|
+
|
|
479
|
+
1. **Generic Messages**: For authentication, use generic messages to avoid user enumeration
|
|
480
|
+
2. **Rate Limiting**: Implement rate limiting to prevent brute force
|
|
481
|
+
3. **Logging**: Log all authentication failures for security monitoring
|
|
482
|
+
4. **Error Details**: Only expose detailed errors in development, not production
|
|
483
|
+
5. **Sensitive Data**: Never include passwords or tokens in error logs
|
|
484
|
+
|
|
485
|
+
## Examples
|
|
486
|
+
|
|
487
|
+
### Complete Login with Error Handling
|
|
488
|
+
|
|
489
|
+
```javascript
|
|
490
|
+
router.post('/mbkauthe/api/login', async (req, res) => {
|
|
491
|
+
const { username, password } = req.body;
|
|
492
|
+
|
|
493
|
+
try {
|
|
494
|
+
// Validation
|
|
495
|
+
if (!username || !password) {
|
|
496
|
+
logError('Login', ErrorCodes.MISSING_REQUIRED_FIELD);
|
|
497
|
+
return res.status(400).json(
|
|
498
|
+
createErrorResponse(400, ErrorCodes.MISSING_REQUIRED_FIELD)
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
if (username.length > 255) {
|
|
503
|
+
logError('Login', ErrorCodes.INVALID_USERNAME_FORMAT, { username });
|
|
504
|
+
return res.status(400).json(
|
|
505
|
+
createErrorResponse(400, ErrorCodes.INVALID_USERNAME_FORMAT)
|
|
506
|
+
);
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
if (password.length < 8) {
|
|
510
|
+
logError('Login', ErrorCodes.INVALID_PASSWORD_LENGTH);
|
|
511
|
+
return res.status(400).json(
|
|
512
|
+
createErrorResponse(400, ErrorCodes.INVALID_PASSWORD_LENGTH)
|
|
513
|
+
);
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// Authentication
|
|
517
|
+
const user = await findUser(username);
|
|
518
|
+
|
|
519
|
+
if (!user) {
|
|
520
|
+
logError('Login', ErrorCodes.USER_NOT_FOUND, { username });
|
|
521
|
+
// Use generic message for security
|
|
522
|
+
return res.status(401).json(
|
|
523
|
+
createErrorResponse(401, ErrorCodes.INVALID_CREDENTIALS)
|
|
524
|
+
);
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
if (!await verifyPassword(password, user.password)) {
|
|
528
|
+
logError('Login', ErrorCodes.INCORRECT_PASSWORD, { username });
|
|
529
|
+
return res.status(401).json(
|
|
530
|
+
createErrorResponse(401, ErrorCodes.INCORRECT_PASSWORD)
|
|
531
|
+
);
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
if (!user.active) {
|
|
535
|
+
logError('Login', ErrorCodes.ACCOUNT_INACTIVE, { username });
|
|
536
|
+
return res.status(403).json(
|
|
537
|
+
createErrorResponse(403, ErrorCodes.ACCOUNT_INACTIVE)
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// Success
|
|
542
|
+
res.json({ success: true, sessionId: '...' });
|
|
543
|
+
|
|
544
|
+
} catch (error) {
|
|
545
|
+
console.error('[mbkauthe] Login error:', error);
|
|
546
|
+
return res.status(500).json(
|
|
547
|
+
createErrorResponse(500, ErrorCodes.INTERNAL_SERVER_ERROR)
|
|
548
|
+
);
|
|
549
|
+
}
|
|
550
|
+
});
|
|
551
|
+
```
|
|
552
|
+
|
|
553
|
+
## Support
|
|
554
|
+
|
|
555
|
+
For questions about error handling:
|
|
556
|
+
- [GitHub Issues](https://github.com/MIbnEKhalid/mbkauthe/issues)
|
|
557
|
+
- Email: support@mbktech.org
|