mbkauthe 2.4.0 → 3.0.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/.env.example +1 -1
- package/LICENSE +339 -373
- package/README.md +116 -288
- package/docs/api.md +139 -1
- package/docs/db.md +35 -5
- package/docs/env.md +32 -0
- package/docs/error-messages.md +557 -0
- package/index.d.ts +233 -0
- package/index.js +43 -32
- package/lib/config/cookies.js +52 -0
- package/lib/{config.js → config/index.js} +21 -85
- package/lib/config/security.js +8 -0
- package/lib/{pool.js → database/pool.js} +1 -1
- package/lib/main.js +28 -964
- 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 +272 -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 +6 -3
- package/public/main.js +4 -4
- package/views/Error/dError.handlebars +1 -1
- package/views/errorCodes.handlebars +341 -0
- package/views/info.handlebars +4 -0
- package/views/loginmbkauthe.handlebars +3 -31
- package/views/showmessage.handlebars +10 -18
package/README.md
CHANGED
|
@@ -1,27 +1,34 @@
|
|
|
1
|
-
# MBKAuthe - Authentication System for Node.js
|
|
1
|
+
# MBKAuthe v3.0 - Authentication System for Node.js
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/mbkauthe)
|
|
4
|
-
[](LICENSE)
|
|
5
5
|
[](https://nodejs.org/)
|
|
6
6
|
[](https://github.com/MIbnEKhalid/mbkauthe/actions/workflows/publish.yml)
|
|
7
7
|
[](https://github.com/MIbnEKhalid/mbkauthe/actions/workflows/codeql.yml)
|
|
8
8
|
|
|
9
|
-
**MBKAuth** is a reusable, production-ready authentication system for Node.js applications built by MBKTech.org. It provides secure session management, two-factor authentication (2FA), role-based access control, and multi-application support out of the box.
|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
<p align="center">
|
|
11
|
+
<img height="64px" src="./public/icon.svg" alt="MBK Chat Platform" />
|
|
12
|
+
</p>
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
-
|
|
23
|
-
-
|
|
24
|
-
-
|
|
14
|
+
<p align="center">
|
|
15
|
+
<img src="https://skillicons.dev/icons?i=nodejs,express,postgres" />
|
|
16
|
+
<img height="48px" src="https://handlebarsjs.com/handlebars-icon.svg" alt="Handlebars" />
|
|
17
|
+
</p>
|
|
18
|
+
|
|
19
|
+
**MBKAuth v3.0** is a production-ready authentication system for Node.js applications. Built with Express and PostgreSQL, it provides secure authentication, 2FA, role-based access, and GitHub OAuth out of the box.
|
|
20
|
+
|
|
21
|
+
## ✨ Key Features
|
|
22
|
+
|
|
23
|
+
- 🔐 Secure password authentication with PBKDF2 hashing
|
|
24
|
+
- 🔑 PostgreSQL session management with cross-subdomain support
|
|
25
|
+
- 📱 Optional TOTP-based 2FA with trusted device memory
|
|
26
|
+
- 🔄 GitHub OAuth integration
|
|
27
|
+
- 👥 Role-based access control (SuperAdmin, NormalUser, Guest)
|
|
28
|
+
- 🎯 Multi-application user management
|
|
29
|
+
- 🛡️ CSRF protection & rate limiting
|
|
30
|
+
- 🚀 Easy Express.js integration
|
|
31
|
+
- 🎨 Customizable Handlebars templates
|
|
25
32
|
|
|
26
33
|
## 📦 Installation
|
|
27
34
|
|
|
@@ -31,72 +38,51 @@ npm install mbkauthe
|
|
|
31
38
|
|
|
32
39
|
## 🚀 Quick Start
|
|
33
40
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
Create a `.env` file in your project root:
|
|
41
|
+
**1. Configure Environment (.env)**
|
|
37
42
|
|
|
38
43
|
```env
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
MAIN_SECRET_TOKEN=your-api-secret-token
|
|
44
|
+
APP_NAME=your-app
|
|
45
|
+
SESSION_SECRET_KEY=your-secret-key
|
|
46
|
+
MAIN_SECRET_TOKEN=api-token
|
|
43
47
|
IS_DEPLOYED=false
|
|
44
48
|
DOMAIN=localhost
|
|
49
|
+
LOGIN_DB=postgresql://user:pass@localhost:5432/db
|
|
45
50
|
|
|
46
|
-
#
|
|
47
|
-
LOGIN_DB=postgresql://username:password@localhost:5432/database_name
|
|
48
|
-
|
|
49
|
-
# Optional Features
|
|
51
|
+
# Optional
|
|
50
52
|
MBKAUTH_TWO_FA_ENABLE=false
|
|
51
53
|
COOKIE_EXPIRE_TIME=2
|
|
52
|
-
DEVICE_TRUST_DURATION_DAYS=7
|
|
53
|
-
|
|
54
|
-
# GitHub OAuth (Optional)
|
|
55
54
|
GITHUB_LOGIN_ENABLED=false
|
|
56
|
-
GITHUB_CLIENT_ID=your-github-oauth-client-id
|
|
57
|
-
GITHUB_CLIENT_SECRET=your-github-oauth-client-secret
|
|
58
55
|
```
|
|
59
56
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
### 2. Set Up Database
|
|
63
|
-
|
|
64
|
-
Create the required tables in your PostgreSQL database. See [Database Structure Documentation](docs/db.md) for complete schemas.
|
|
57
|
+
**2. Set Up Database**
|
|
65
58
|
|
|
66
59
|
```sql
|
|
67
|
-
-- Users table
|
|
68
60
|
CREATE TYPE role AS ENUM ('SuperAdmin', 'NormalUser', 'Guest');
|
|
69
61
|
|
|
70
62
|
CREATE TABLE "Users" (
|
|
71
63
|
id SERIAL PRIMARY KEY,
|
|
72
64
|
"UserName" VARCHAR(50) NOT NULL UNIQUE,
|
|
73
65
|
"Password" VARCHAR(61) NOT NULL,
|
|
74
|
-
"Role" role DEFAULT 'NormalUser'
|
|
66
|
+
"Role" role DEFAULT 'NormalUser',
|
|
75
67
|
"Active" BOOLEAN DEFAULT FALSE,
|
|
76
68
|
"AllowedApps" JSONB DEFAULT '["mbkauthe"]',
|
|
77
69
|
"SessionId" VARCHAR(213),
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
"last_login" TIMESTAMP WITH TIME ZONE
|
|
70
|
+
created_at TIMESTAMP DEFAULT NOW(),
|
|
71
|
+
updated_at TIMESTAMP DEFAULT NOW()
|
|
81
72
|
);
|
|
82
|
-
|
|
83
|
-
-- Session table (created automatically by connect-pg-simple)
|
|
84
|
-
-- TwoFA table (optional, if 2FA is enabled)
|
|
85
|
-
-- TrustedDevices table (optional, for "Remember this device" feature)
|
|
86
|
-
-- user_github table (optional, for GitHub OAuth integration)
|
|
87
73
|
```
|
|
88
74
|
|
|
89
|
-
|
|
75
|
+
See [docs/db.md](docs/db.md) for complete schemas.
|
|
76
|
+
|
|
77
|
+
**3. Integrate with Express**
|
|
90
78
|
|
|
91
79
|
```javascript
|
|
92
80
|
import express from 'express';
|
|
93
|
-
import mbkauthe from 'mbkauthe';
|
|
94
|
-
import { validateSession, checkRolePermission } from 'mbkauthe';
|
|
81
|
+
import mbkauthe, { validateSession, checkRolePermission } from 'mbkauthe';
|
|
95
82
|
import dotenv from 'dotenv';
|
|
96
83
|
|
|
97
84
|
dotenv.config();
|
|
98
85
|
|
|
99
|
-
// Set mbkauthe configuration
|
|
100
86
|
process.env.mbkautheVar = JSON.stringify({
|
|
101
87
|
APP_NAME: process.env.APP_NAME,
|
|
102
88
|
SESSION_SECRET_KEY: process.env.SESSION_SECRET_KEY,
|
|
@@ -104,126 +90,84 @@ process.env.mbkautheVar = JSON.stringify({
|
|
|
104
90
|
IS_DEPLOYED: process.env.IS_DEPLOYED,
|
|
105
91
|
DOMAIN: process.env.DOMAIN,
|
|
106
92
|
LOGIN_DB: process.env.LOGIN_DB,
|
|
107
|
-
|
|
108
|
-
COOKIE_EXPIRE_TIME: process.env.COOKIE_EXPIRE_TIME || 2,
|
|
109
|
-
DEVICE_TRUST_DURATION_DAYS: process.env.DEVICE_TRUST_DURATION_DAYS || 7,
|
|
110
|
-
GITHUB_LOGIN_ENABLED: process.env.GITHUB_LOGIN_ENABLED,
|
|
111
|
-
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,
|
|
112
|
-
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET,
|
|
113
|
-
loginRedirectURL: '/dashboard' // Redirect after successful login
|
|
93
|
+
loginRedirectURL: '/dashboard'
|
|
114
94
|
});
|
|
115
95
|
|
|
116
96
|
const app = express();
|
|
117
97
|
|
|
118
|
-
// Mount
|
|
98
|
+
// Mount authentication routes
|
|
119
99
|
app.use(mbkauthe);
|
|
120
100
|
|
|
121
|
-
// Protected
|
|
101
|
+
// Protected routes
|
|
122
102
|
app.get('/dashboard', validateSession, (req, res) => {
|
|
123
103
|
res.send(`Welcome ${req.session.user.username}!`);
|
|
124
104
|
});
|
|
125
105
|
|
|
126
|
-
// Role-based route protection
|
|
127
106
|
app.get('/admin', validateSession, checkRolePermission(['SuperAdmin']), (req, res) => {
|
|
128
|
-
res.send('Admin
|
|
107
|
+
res.send('Admin Panel');
|
|
129
108
|
});
|
|
130
109
|
|
|
131
|
-
app.listen(3000
|
|
132
|
-
console.log('Server running on http://localhost:3000');
|
|
133
|
-
});
|
|
110
|
+
app.listen(3000);
|
|
134
111
|
```
|
|
135
112
|
|
|
136
|
-
##
|
|
137
|
-
|
|
138
|
-
### Middleware Functions
|
|
139
|
-
|
|
140
|
-
#### `validateSession`
|
|
141
|
-
Validates that a user has an active session. Redirects to login if not authenticated.
|
|
113
|
+
## 📂 Architecture (v3.0)
|
|
142
114
|
|
|
143
|
-
```
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
115
|
+
```
|
|
116
|
+
lib/
|
|
117
|
+
├── config/ # Configuration & security
|
|
118
|
+
├── database/ # PostgreSQL pool
|
|
119
|
+
├── utils/ # Errors & response helpers
|
|
120
|
+
├── middleware/ # Auth & session middleware
|
|
121
|
+
└── routes/ # Auth, OAuth, misc routes
|
|
148
122
|
```
|
|
149
123
|
|
|
150
|
-
|
|
151
|
-
|
|
124
|
+
**Key Improvements in v3.0:**
|
|
125
|
+
- Modular structure with clear separation of concerns
|
|
126
|
+
- Organized config, database, utils, middleware, and routes
|
|
127
|
+
- Better maintainability and scalability
|
|
152
128
|
|
|
153
|
-
|
|
154
|
-
app.get('/admin', validateSession, checkRolePermission(['SuperAdmin']), (req, res) => {
|
|
155
|
-
// Only SuperAdmin can access
|
|
156
|
-
});
|
|
157
|
-
```
|
|
129
|
+
## 🔧 Core API
|
|
158
130
|
|
|
159
|
-
|
|
160
|
-
Combined middleware for session validation and role checking.
|
|
131
|
+
### Middleware
|
|
161
132
|
|
|
162
133
|
```javascript
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
});
|
|
166
|
-
```
|
|
134
|
+
// Session validation
|
|
135
|
+
app.get('/protected', validateSession, handler);
|
|
167
136
|
|
|
168
|
-
|
|
169
|
-
|
|
137
|
+
// Role checking
|
|
138
|
+
app.get('/admin', validateSession, checkRolePermission(['SuperAdmin']), handler);
|
|
170
139
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
});
|
|
175
|
-
```
|
|
140
|
+
// Combined
|
|
141
|
+
import { validateSessionAndRole } from 'mbkauthe';
|
|
142
|
+
app.get('/mod', validateSessionAndRole(['SuperAdmin', 'NormalUser']), handler);
|
|
176
143
|
|
|
177
|
-
|
|
144
|
+
// API token auth
|
|
145
|
+
import { authenticate } from 'mbkauthe';
|
|
146
|
+
app.post('/api/data', authenticate(process.env.API_TOKEN), handler);
|
|
147
|
+
```
|
|
178
148
|
|
|
179
|
-
|
|
149
|
+
### Built-in Routes
|
|
180
150
|
|
|
181
151
|
- `GET /mbkauthe/login` - Login page
|
|
182
|
-
- `POST /mbkauthe/api/login` - Login endpoint
|
|
183
|
-
- `POST /mbkauthe/api/logout` - Logout endpoint
|
|
184
|
-
- `GET /mbkauthe/2fa` -
|
|
185
|
-
- `POST /mbkauthe/api/verify-2fa` - 2FA verification
|
|
186
|
-
- `GET /mbkauthe/api/github/login` -
|
|
187
|
-
- `GET /mbkauthe/
|
|
188
|
-
- `GET /mbkauthe/
|
|
189
|
-
- `POST /mbkauthe/api/terminateAllSessions` - Terminate all active sessions (authenticated)
|
|
152
|
+
- `POST /mbkauthe/api/login` - Login endpoint (8/min rate limit)
|
|
153
|
+
- `POST /mbkauthe/api/logout` - Logout endpoint (10/min rate limit)
|
|
154
|
+
- `GET /mbkauthe/2fa` - 2FA page (if enabled)
|
|
155
|
+
- `POST /mbkauthe/api/verify-2fa` - 2FA verification (5/min rate limit)
|
|
156
|
+
- `GET /mbkauthe/api/github/login` - GitHub OAuth
|
|
157
|
+
- `GET /mbkauthe/info` - Version & config info
|
|
158
|
+
- `GET /mbkauthe/ErrorCode` - Error documentation
|
|
190
159
|
|
|
191
160
|
## 🔐 Security Features
|
|
192
161
|
|
|
193
|
-
|
|
194
|
-
- **
|
|
195
|
-
- **
|
|
196
|
-
- **
|
|
197
|
-
- **
|
|
198
|
-
|
|
199
|
-
### CSRF Protection
|
|
200
|
-
All POST routes are protected with CSRF tokens. CSRF tokens are automatically included in rendered forms.
|
|
201
|
-
|
|
202
|
-
### Password Hashing
|
|
203
|
-
Passwords are hashed using bcrypt with a secure salt. Set `EncryptedPassword: "true"` in `mbkautheVar` to enable.
|
|
204
|
-
|
|
205
|
-
### Secure Cookies
|
|
206
|
-
- `httpOnly` flag prevents XSS attacks
|
|
207
|
-
- `sameSite: 'lax'` prevents CSRF attacks
|
|
208
|
-
- `secure` flag in production ensures HTTPS-only cookies
|
|
209
|
-
- Configurable expiration time
|
|
210
|
-
|
|
211
|
-
### Session Management
|
|
212
|
-
- PostgreSQL-backed persistent sessions
|
|
213
|
-
- Automatic session cleanup
|
|
214
|
-
- Session restoration from cookies
|
|
215
|
-
- Cross-subdomain session sharing (when deployed)
|
|
162
|
+
- **Rate Limiting**: Login (8/min), Logout (10/min), 2FA (5/min), OAuth (10/5min)
|
|
163
|
+
- **CSRF Protection**: All POST routes protected
|
|
164
|
+
- **Secure Cookies**: httpOnly, sameSite, secure in production
|
|
165
|
+
- **Password Hashing**: PBKDF2 with 100k iterations
|
|
166
|
+
- **Session Security**: PostgreSQL-backed, automatic cleanup
|
|
216
167
|
|
|
217
168
|
## 📱 Two-Factor Authentication
|
|
218
169
|
|
|
219
|
-
Enable
|
|
220
|
-
|
|
221
|
-
1. User logs in with username/password
|
|
222
|
-
2. If 2FA is enabled for the user, they're prompted for a 6-digit code
|
|
223
|
-
3. Code is verified using TOTP (Time-based One-Time Password)
|
|
224
|
-
4. Session is established after successful 2FA
|
|
225
|
-
|
|
226
|
-
### Database Setup for 2FA
|
|
170
|
+
Enable with `MBKAUTH_TWO_FA_ENABLE=true`:
|
|
227
171
|
|
|
228
172
|
```sql
|
|
229
173
|
CREATE TABLE "TwoFA" (
|
|
@@ -233,27 +177,20 @@ CREATE TABLE "TwoFA" (
|
|
|
233
177
|
);
|
|
234
178
|
```
|
|
235
179
|
|
|
236
|
-
|
|
180
|
+
Users can mark devices as trusted to skip 2FA for configurable duration.
|
|
237
181
|
|
|
238
|
-
|
|
239
|
-
Users can log in using their GitHub accounts if they have previously linked their GitHub account to their MBKAuth account.
|
|
182
|
+
## 🔄 GitHub OAuth
|
|
240
183
|
|
|
241
|
-
|
|
184
|
+
**Setup:**
|
|
242
185
|
|
|
243
|
-
1.
|
|
244
|
-
|
|
245
|
-
- Create a new OAuth App
|
|
246
|
-
- Set callback URL: `https://yourdomain.com/mbkauthe/api/github/login/callback`
|
|
247
|
-
- Copy Client ID and Client Secret
|
|
248
|
-
|
|
249
|
-
2. **Configure Environment**:
|
|
186
|
+
1. Create GitHub OAuth App with callback: `https://yourdomain.com/mbkauthe/api/github/login/callback`
|
|
187
|
+
2. Configure environment:
|
|
250
188
|
```env
|
|
251
189
|
GITHUB_LOGIN_ENABLED=true
|
|
252
|
-
GITHUB_CLIENT_ID=
|
|
253
|
-
GITHUB_CLIENT_SECRET=
|
|
190
|
+
GITHUB_CLIENT_ID=your_client_id
|
|
191
|
+
GITHUB_CLIENT_SECRET=your_client_secret
|
|
254
192
|
```
|
|
255
|
-
|
|
256
|
-
3. **Database Setup**:
|
|
193
|
+
3. Create table:
|
|
257
194
|
```sql
|
|
258
195
|
CREATE TABLE user_github (
|
|
259
196
|
id SERIAL PRIMARY KEY,
|
|
@@ -261,180 +198,71 @@ CREATE TABLE user_github (
|
|
|
261
198
|
github_id VARCHAR(255) UNIQUE,
|
|
262
199
|
github_username VARCHAR(255),
|
|
263
200
|
access_token VARCHAR(255),
|
|
264
|
-
created_at TIMESTAMP
|
|
265
|
-
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
|
|
201
|
+
created_at TIMESTAMP DEFAULT NOW()
|
|
266
202
|
);
|
|
267
|
-
|
|
268
|
-
CREATE INDEX idx_user_github_github_id ON user_github (github_id);
|
|
269
|
-
CREATE INDEX idx_user_github_user_name ON user_github (user_name);
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### How It Works
|
|
273
|
-
|
|
274
|
-
1. User clicks "Login with GitHub" on the login page
|
|
275
|
-
2. User authenticates with GitHub
|
|
276
|
-
3. System verifies the GitHub account is linked to an active user
|
|
277
|
-
4. If 2FA is enabled, user is prompted for 2FA code
|
|
278
|
-
5. Session is established upon successful authentication
|
|
279
|
-
|
|
280
|
-
### Routes
|
|
281
|
-
|
|
282
|
-
- `GET /mbkauthe/api/github/login` - Initiates GitHub OAuth flow
|
|
283
|
-
- `GET /mbkauthe/api/github/login/callback` - Handles OAuth callback
|
|
284
|
-
|
|
285
|
-
## 🖥️ Trusted Devices (Remember Device)
|
|
286
|
-
|
|
287
|
-
### Overview
|
|
288
|
-
The "Remember this device" feature allows users to skip 2FA verification on trusted devices for a configurable duration.
|
|
289
|
-
|
|
290
|
-
### Configuration
|
|
291
|
-
|
|
292
|
-
```env
|
|
293
|
-
# Duration in days before device trust expires (default: 7 days)
|
|
294
|
-
DEVICE_TRUST_DURATION_DAYS=7
|
|
295
|
-
```
|
|
296
|
-
|
|
297
|
-
### Database Setup
|
|
298
|
-
|
|
299
|
-
```sql
|
|
300
|
-
CREATE TABLE "TrustedDevices" (
|
|
301
|
-
"id" SERIAL PRIMARY KEY,
|
|
302
|
-
"UserName" VARCHAR(50) NOT NULL REFERENCES "Users"("UserName") ON DELETE CASCADE,
|
|
303
|
-
"DeviceToken" VARCHAR(64) UNIQUE NOT NULL,
|
|
304
|
-
"DeviceName" VARCHAR(255),
|
|
305
|
-
"UserAgent" TEXT,
|
|
306
|
-
"IpAddress" VARCHAR(45),
|
|
307
|
-
"CreatedAt" TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP,
|
|
308
|
-
"ExpiresAt" TIMESTAMP WITH TIME ZONE NOT NULL,
|
|
309
|
-
"LastUsed" TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
|
|
310
|
-
);
|
|
311
|
-
|
|
312
|
-
CREATE INDEX idx_trusted_devices_token ON "TrustedDevices"("DeviceToken");
|
|
313
|
-
CREATE INDEX idx_trusted_devices_username ON "TrustedDevices"("UserName");
|
|
314
|
-
CREATE INDEX idx_trusted_devices_expires ON "TrustedDevices"("ExpiresAt");
|
|
315
203
|
```
|
|
316
204
|
|
|
317
|
-
### How It Works
|
|
318
|
-
|
|
319
|
-
1. After successful login and 2FA verification, user can check "Remember this device"
|
|
320
|
-
2. A secure device token is generated and stored in cookies
|
|
321
|
-
3. On subsequent logins from the same device, 2FA is skipped
|
|
322
|
-
4. Device trust expires after configured duration
|
|
323
|
-
5. Users can manage trusted devices through their account settings
|
|
324
|
-
|
|
325
|
-
### Security Notes
|
|
326
|
-
|
|
327
|
-
- Device tokens are cryptographically secure (64-byte random tokens)
|
|
328
|
-
- Tokens automatically expire after the configured duration
|
|
329
|
-
- Last used timestamp is tracked for auditing
|
|
330
|
-
- IP address and user agent are stored for security monitoring
|
|
331
|
-
- Devices can be manually revoked by users
|
|
332
|
-
|
|
333
205
|
## 🎨 Customization
|
|
334
206
|
|
|
335
|
-
|
|
336
|
-
Set `loginRedirectURL` in `mbkautheVar`:
|
|
337
|
-
|
|
207
|
+
**Redirect URL:**
|
|
338
208
|
```javascript
|
|
339
209
|
process.env.mbkautheVar = JSON.stringify({
|
|
340
|
-
// ...
|
|
341
|
-
loginRedirectURL: '/dashboard'
|
|
210
|
+
// ...
|
|
211
|
+
loginRedirectURL: '/dashboard'
|
|
342
212
|
});
|
|
343
213
|
```
|
|
344
214
|
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
- `
|
|
348
|
-
- `
|
|
349
|
-
- `views/Error/dError.handlebars` - Error page
|
|
350
|
-
|
|
351
|
-
### Database Pool Access
|
|
352
|
-
Access the database pool for custom queries:
|
|
215
|
+
**Custom Views:** Create in `views/` directory:
|
|
216
|
+
- `loginmbkauthe.handlebars` - Login page
|
|
217
|
+
- `2fa.handlebars` - 2FA page
|
|
218
|
+
- `Error/dError.handlebars` - Error page
|
|
353
219
|
|
|
220
|
+
**Database Access:**
|
|
354
221
|
```javascript
|
|
355
222
|
import { dblogin } from 'mbkauthe';
|
|
356
|
-
|
|
357
|
-
const result = await dblogin.query('SELECT * FROM "Users" WHERE "UserName" = $1', [username]);
|
|
223
|
+
const result = await dblogin.query('SELECT * FROM "Users"');
|
|
358
224
|
```
|
|
359
225
|
|
|
360
226
|
## 🚢 Deployment
|
|
361
227
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
228
|
+
**Production Checklist:**
|
|
229
|
+
- ✅ Set `IS_DEPLOYED=true`
|
|
230
|
+
- ✅ Use strong secrets for SESSION_SECRET_KEY and Main_SECRET_TOKEN
|
|
231
|
+
- ✅ Enable HTTPS
|
|
232
|
+
- ✅ Configure correct DOMAIN
|
|
233
|
+
- ✅ Set appropriate COOKIE_EXPIRE_TIME
|
|
234
|
+
- ✅ Use environment variables for all secrets
|
|
365
235
|
|
|
236
|
+
**Vercel:**
|
|
366
237
|
```json
|
|
367
238
|
{
|
|
368
239
|
"version": 2,
|
|
369
|
-
"builds": [
|
|
370
|
-
|
|
371
|
-
"src": "index.js",
|
|
372
|
-
"use": "@vercel/node"
|
|
373
|
-
}
|
|
374
|
-
],
|
|
375
|
-
"routes": [
|
|
376
|
-
{
|
|
377
|
-
"src": "/(.*)",
|
|
378
|
-
"dest": "/index.js"
|
|
379
|
-
}
|
|
380
|
-
]
|
|
240
|
+
"builds": [{ "src": "index.js", "use": "@vercel/node" }],
|
|
241
|
+
"routes": [{ "src": "/(.*)", "dest": "/index.js" }]
|
|
381
242
|
}
|
|
382
243
|
```
|
|
383
244
|
|
|
384
|
-
### Production Checklist
|
|
385
|
-
|
|
386
|
-
- [ ] Set `IS_DEPLOYED=true`
|
|
387
|
-
- [ ] Use a strong `SESSION_SECRET_KEY` and `Main_SECRET_TOKEN`
|
|
388
|
-
- [ ] Enable HTTPS
|
|
389
|
-
- [ ] Set correct `DOMAIN`
|
|
390
|
-
- [ ] Enable 2FA for sensitive applications
|
|
391
|
-
- [ ] Configure `DEVICE_TRUST_DURATION_DAYS` appropriately
|
|
392
|
-
- [ ] Set up GitHub OAuth if using GitHub login
|
|
393
|
-
- [ ] Use environment variables for all secrets
|
|
394
|
-
- [ ] Set appropriate `COOKIE_EXPIRE_TIME`
|
|
395
|
-
- [ ] Configure PostgreSQL with proper security and indexes
|
|
396
|
-
- [ ] Enable password hashing with bcrypt
|
|
397
|
-
- [ ] Regularly audit and clean up expired trusted devices
|
|
398
|
-
|
|
399
245
|
## 📚 Documentation
|
|
400
246
|
|
|
401
|
-
- [API Documentation](docs/api.md) - Complete API reference
|
|
402
|
-
- [
|
|
403
|
-
- [
|
|
404
|
-
|
|
405
|
-
## 🔄 Version Check
|
|
406
|
-
|
|
407
|
-
MBKAuth automatically checks for updates on startup and warns if a newer version is available. Keep your package updated for security patches.
|
|
408
|
-
|
|
409
|
-
## 🤝 Contributing
|
|
410
|
-
|
|
411
|
-
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
412
|
-
|
|
413
|
-
1. Fork the repository
|
|
414
|
-
2. Create your feature branch (`git checkout -b feature/AmazingFeature`)
|
|
415
|
-
3. Commit your changes (`git commit -m 'Add some AmazingFeature'`)
|
|
416
|
-
4. Push to the branch (`git push origin feature/AmazingFeature`)
|
|
417
|
-
5. Open a Pull Request
|
|
247
|
+
- [API Documentation](docs/api.md) - Complete API reference
|
|
248
|
+
- [Database Guide](docs/db.md) - Schema details
|
|
249
|
+
- [Environment Config](docs/env.md) - Configuration options
|
|
418
250
|
|
|
419
251
|
## 📝 License
|
|
420
252
|
|
|
421
|
-
|
|
253
|
+
GNU General Public License v2.0 - see [LICENSE](LICENSE)
|
|
422
254
|
|
|
423
255
|
## 👨💻 Author
|
|
424
256
|
|
|
425
257
|
**Muhammad Bin Khalid**
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
## 🐛 Issues & Support
|
|
430
|
-
|
|
431
|
-
Found a bug or need help? Please [open an issue](https://github.com/MIbnEKhalid/mbkauthe/issues) on GitHub.
|
|
258
|
+
📧 [support@mbktech.org](mailto:support@mbktech.org) | [chmuhammadbinkhalid28@gmail.com](mailto:chmuhammadbinkhalid28@gmail.com)
|
|
259
|
+
🔗 [@MIbnEKhalid](https://github.com/MIbnEKhalid)
|
|
432
260
|
|
|
433
261
|
## 🔗 Links
|
|
434
262
|
|
|
435
263
|
- [npm Package](https://www.npmjs.com/package/mbkauthe)
|
|
436
264
|
- [GitHub Repository](https://github.com/MIbnEKhalid/mbkauthe)
|
|
437
|
-
- [
|
|
265
|
+
- [Issues & Support](https://github.com/MIbnEKhalid/mbkauthe/issues)
|
|
438
266
|
|
|
439
267
|
---
|
|
440
268
|
|