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