@dax-crafta/auth 2.1.5 → 2.1.6
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 +202 -126
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,76 +1,169 @@
|
|
|
1
1
|
# @dax-crafta/auth
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Production-ready auth for Crafta apps with plug-and-play defaults and clean JSON customization.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@dax-crafta/auth)
|
|
6
6
|
[](https://github.com/daxp472/crafta/blob/main/LICENSE)
|
|
7
7
|
[](https://www.npmjs.com/package/@dax-crafta/auth)
|
|
8
8
|
|
|
9
|
-
##
|
|
9
|
+
## Why this package
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
- JWT-based session management
|
|
15
|
-
- Refresh token rotation
|
|
11
|
+
- You should not rebuild auth from scratch for every project.
|
|
12
|
+
- This package gives you login, refresh tokens, password reset, RBAC, 2FA, audit logs, and feature toggles out of the box.
|
|
13
|
+
- Keep defaults for speed, customize only where needed.
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
- Custom role creation
|
|
19
|
-
- Granular permissions
|
|
20
|
-
- Resource-based access control
|
|
21
|
-
- Role hierarchy support
|
|
15
|
+
## What you get
|
|
22
16
|
|
|
23
|
-
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
17
|
+
- JWT auth with refresh token rotation
|
|
18
|
+
- Register, login, verify, forgot/reset password routes
|
|
19
|
+
- Optional 2FA (TOTP + backup codes)
|
|
20
|
+
- RBAC with role and permission service
|
|
21
|
+
- Route-level rate limiting
|
|
22
|
+
- Password policy and password history checks
|
|
23
|
+
- Optional Google OAuth
|
|
24
|
+
- Audit logs (Mongo + file)
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
- Email verification
|
|
31
|
-
- Password reset
|
|
32
|
-
- Login notifications
|
|
33
|
-
- Custom email templates
|
|
34
|
-
|
|
35
|
-
- 📝 **Audit Logging**
|
|
36
|
-
- Detailed activity tracking
|
|
37
|
-
- Security event logging
|
|
38
|
-
- User session monitoring
|
|
39
|
-
|
|
40
|
-
## Quick Start
|
|
26
|
+
## 1. Install
|
|
41
27
|
|
|
42
28
|
```bash
|
|
43
|
-
npm install @dax-crafta/auth
|
|
29
|
+
npm install crafta @dax-crafta/auth
|
|
44
30
|
```
|
|
45
31
|
|
|
46
|
-
|
|
32
|
+
## 2. Minimal setup (copy-paste)
|
|
33
|
+
|
|
34
|
+
Create `server.js`:
|
|
35
|
+
|
|
36
|
+
```js
|
|
47
37
|
const { crafta } = require('crafta');
|
|
48
38
|
const { auth } = require('@dax-crafta/auth');
|
|
49
39
|
|
|
50
40
|
const app = crafta();
|
|
51
41
|
|
|
52
|
-
// Basic setup
|
|
53
42
|
auth({
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
43
|
+
env: {
|
|
44
|
+
JWT_SECRET: process.env.JWT_SECRET || 'dev-secret-123'
|
|
45
|
+
},
|
|
46
|
+
mongoUrl: process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/crafta-auth-demo',
|
|
47
|
+
|
|
48
|
+
// Local dev ke liye simple mode
|
|
49
|
+
features: {
|
|
50
|
+
emailVerification: false,
|
|
51
|
+
loginAlerts: false,
|
|
52
|
+
csrf: false
|
|
53
|
+
}
|
|
59
54
|
})(app);
|
|
60
55
|
|
|
61
|
-
app.
|
|
56
|
+
app.get('/health', (req, res) => {
|
|
57
|
+
res.json({ ok: true, message: 'Auth is running' });
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
app.listen(3000, () => {
|
|
61
|
+
console.log('Server running on http://localhost:3000');
|
|
62
|
+
});
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
Run it:
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
node server.js
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## 3. First Postman test flow
|
|
72
|
+
|
|
73
|
+
### Health
|
|
74
|
+
- `GET /health`
|
|
75
|
+
|
|
76
|
+
### Register
|
|
77
|
+
- `POST /register`
|
|
78
|
+
|
|
79
|
+
```json
|
|
80
|
+
{
|
|
81
|
+
"name": "Test User",
|
|
82
|
+
"email": "testuser1@example.com",
|
|
83
|
+
"password": "Strong@1234"
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Login
|
|
88
|
+
- `POST /login`
|
|
89
|
+
|
|
90
|
+
```json
|
|
91
|
+
{
|
|
92
|
+
"email": "testuser1@example.com",
|
|
93
|
+
"password": "Strong@1234"
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Use returned `accessToken` on protected routes:
|
|
98
|
+
- `Authorization: Bearer <token>`
|
|
99
|
+
|
|
100
|
+
### Profile (protected)
|
|
101
|
+
- `PUT /profile`
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"name": "Updated Name"
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### Refresh token
|
|
110
|
+
- `POST /refresh-token`
|
|
111
|
+
|
|
112
|
+
```json
|
|
113
|
+
{
|
|
114
|
+
"refreshToken": "<token-from-login>"
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## 4. Customize with JSON (main power)
|
|
119
|
+
|
|
120
|
+
Everything is controlled from one config object.
|
|
121
|
+
|
|
122
|
+
```js
|
|
123
|
+
auth({
|
|
124
|
+
strategy: 'jwt',
|
|
125
|
+
fields: ['name', 'email', 'password', 'age'],
|
|
126
|
+
|
|
127
|
+
routes: {
|
|
128
|
+
register: '/api/auth/register',
|
|
129
|
+
login: '/api/auth/login',
|
|
130
|
+
verify: '/api/auth/verify',
|
|
131
|
+
forgotPassword: '/api/auth/forgot-password',
|
|
132
|
+
resetPassword: '/api/auth/reset-password',
|
|
133
|
+
refreshToken: '/api/auth/refresh-token',
|
|
134
|
+
profile: '/api/auth/profile',
|
|
135
|
+
twoFactor: '/api/auth/2fa'
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
passwordPolicy: {
|
|
139
|
+
minLength: 10,
|
|
140
|
+
requireUppercase: true,
|
|
141
|
+
requireLowercase: true,
|
|
142
|
+
requireNumbers: true,
|
|
143
|
+
requireSpecialChars: true,
|
|
144
|
+
expiryDays: 90,
|
|
145
|
+
minStrength: 3
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
limits: {
|
|
149
|
+
loginMax: 10,
|
|
150
|
+
twoFAMax: 20,
|
|
151
|
+
forgotPasswordMax: 5,
|
|
152
|
+
refreshMax: 30
|
|
153
|
+
}
|
|
154
|
+
})(app);
|
|
62
155
|
```
|
|
63
156
|
|
|
64
|
-
## Feature
|
|
157
|
+
## 5. Feature toggles (fast on/off)
|
|
65
158
|
|
|
66
|
-
|
|
159
|
+
Set any feature to `false` and it turns off.
|
|
67
160
|
|
|
68
|
-
```
|
|
161
|
+
```js
|
|
69
162
|
auth({
|
|
70
163
|
features: {
|
|
71
164
|
emailVerification: false,
|
|
72
165
|
loginAlerts: false,
|
|
73
|
-
securityAttempts:
|
|
166
|
+
securityAttempts: true,
|
|
74
167
|
rateLimit: true,
|
|
75
168
|
auditLogs: true,
|
|
76
169
|
twoFactor: true,
|
|
@@ -79,119 +172,102 @@ auth({
|
|
|
79
172
|
})(app);
|
|
80
173
|
```
|
|
81
174
|
|
|
82
|
-
|
|
175
|
+
Notes:
|
|
176
|
+
- If SMTP is missing, emailVerification/loginAlerts are auto-disabled with warning.
|
|
177
|
+
- If `securityAttempts: false`, lockout attempt handling is disabled.
|
|
178
|
+
|
|
179
|
+
## 6. Email setup (optional)
|
|
83
180
|
|
|
84
|
-
|
|
181
|
+
Enable this only when SMTP is ready:
|
|
85
182
|
|
|
86
|
-
```
|
|
183
|
+
```js
|
|
87
184
|
auth({
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
// User Fields
|
|
92
|
-
fields: ['name', 'email', 'password', 'age'],
|
|
93
|
-
|
|
94
|
-
// Routes Configuration
|
|
95
|
-
routes: {
|
|
96
|
-
register: '/register',
|
|
97
|
-
login: '/login',
|
|
98
|
-
verify: '/verify',
|
|
99
|
-
forgotPassword: '/forgot-password',
|
|
100
|
-
resetPassword: '/reset-password',
|
|
101
|
-
refreshToken: '/refresh-token',
|
|
102
|
-
profile: '/profile',
|
|
103
|
-
twoFactor: '/2fa'
|
|
104
|
-
},
|
|
105
|
-
|
|
106
|
-
// Security Settings
|
|
107
|
-
maxLoginAttempts: 5,
|
|
108
|
-
emailVerification: true,
|
|
109
|
-
loginAlerts: true,
|
|
110
|
-
|
|
111
|
-
// Password Policy
|
|
112
|
-
passwordPolicy: {
|
|
113
|
-
minLength: 8,
|
|
114
|
-
requireUppercase: true,
|
|
115
|
-
requireNumbers: true,
|
|
116
|
-
requireSpecialChars: true,
|
|
117
|
-
expiryDays: 90
|
|
185
|
+
features: {
|
|
186
|
+
emailVerification: true,
|
|
187
|
+
loginAlerts: true
|
|
118
188
|
},
|
|
119
|
-
|
|
120
|
-
// Email Configuration
|
|
121
189
|
smtp: {
|
|
122
|
-
host:
|
|
123
|
-
port: 587,
|
|
190
|
+
host: process.env.SMTP_HOST,
|
|
191
|
+
port: Number(process.env.SMTP_PORT || 587),
|
|
124
192
|
auth: {
|
|
125
|
-
user:
|
|
126
|
-
pass:
|
|
193
|
+
user: process.env.SMTP_USER,
|
|
194
|
+
pass: process.env.SMTP_PASS
|
|
127
195
|
},
|
|
128
|
-
from:
|
|
196
|
+
from: process.env.SMTP_FROM
|
|
129
197
|
},
|
|
130
|
-
|
|
131
|
-
|
|
198
|
+
baseUrl: process.env.BASE_URL || 'http://localhost:3000'
|
|
199
|
+
})(app);
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## 7. Google OAuth setup (optional)
|
|
203
|
+
|
|
204
|
+
```js
|
|
205
|
+
auth({
|
|
132
206
|
social: {
|
|
133
207
|
google: {
|
|
134
|
-
clientID:
|
|
135
|
-
clientSecret:
|
|
208
|
+
clientID: process.env.GOOGLE_CLIENT_ID,
|
|
209
|
+
clientSecret: process.env.GOOGLE_CLIENT_SECRET,
|
|
136
210
|
callbackURL: 'http://localhost:3000/auth/google/callback'
|
|
137
211
|
}
|
|
138
212
|
}
|
|
139
213
|
})(app);
|
|
140
214
|
```
|
|
141
215
|
|
|
142
|
-
|
|
216
|
+
Routes:
|
|
217
|
+
- `GET /auth/google`
|
|
218
|
+
- `GET /auth/google/callback`
|
|
219
|
+
|
|
220
|
+
## 8. RBAC quick use
|
|
143
221
|
|
|
144
|
-
```
|
|
145
|
-
|
|
146
|
-
const adminRole = await roleService.createRole({
|
|
222
|
+
```js
|
|
223
|
+
const role = await app.roleService.createRole({
|
|
147
224
|
name: 'admin',
|
|
148
|
-
permissions: [
|
|
149
|
-
resource: 'users',
|
|
150
|
-
|
|
151
|
-
}]
|
|
225
|
+
permissions: [
|
|
226
|
+
{ resource: 'users', actions: ['create', 'read', 'update', 'delete'] }
|
|
227
|
+
]
|
|
152
228
|
});
|
|
153
229
|
|
|
154
|
-
|
|
155
|
-
const canAccess = await roleService.checkPermission('admin', 'users', 'create');
|
|
230
|
+
const allowed = await app.roleService.checkPermission('admin', 'users', 'delete');
|
|
156
231
|
```
|
|
157
232
|
|
|
158
|
-
##
|
|
233
|
+
## 9. Environment variables
|
|
159
234
|
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
235
|
+
```bash
|
|
236
|
+
JWT_SECRET=my-super-secret
|
|
237
|
+
MONGO_URL=mongodb://127.0.0.1:27017/crafta-auth-demo
|
|
238
|
+
SMTP_HOST=smtp.example.com
|
|
239
|
+
SMTP_PORT=587
|
|
240
|
+
SMTP_USER=you@example.com
|
|
241
|
+
SMTP_PASS=your-password
|
|
242
|
+
SMTP_FROM=noreply@example.com
|
|
243
|
+
BASE_URL=http://localhost:3000
|
|
244
|
+
GOOGLE_CLIENT_ID=...
|
|
245
|
+
GOOGLE_CLIENT_SECRET=...
|
|
169
246
|
```
|
|
170
247
|
|
|
171
|
-
##
|
|
248
|
+
## 10. Troubleshooting
|
|
172
249
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
userId: user.id,
|
|
177
|
-
action: 'login',
|
|
178
|
-
ipAddress: req.ip,
|
|
179
|
-
userAgent: req.headers['user-agent'],
|
|
180
|
-
status: 'success'
|
|
181
|
-
});
|
|
250
|
+
- 401 on protected route
|
|
251
|
+
- Token missing or expired.
|
|
252
|
+
- Send `Authorization: Bearer <accessToken>`.
|
|
182
253
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
254
|
+
- Register fails on password policy
|
|
255
|
+
- Use uppercase + lowercase + number + special char.
|
|
256
|
+
|
|
257
|
+
- Email not sending
|
|
258
|
+
- Check SMTP credentials and port.
|
|
259
|
+
- If SMTP absent, email features auto-disable by design.
|
|
186
260
|
|
|
187
|
-
|
|
261
|
+
- Refresh token invalid
|
|
262
|
+
- Refresh token is rotated; use latest token returned by API.
|
|
188
263
|
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
264
|
+
## 11. Release confidence
|
|
265
|
+
|
|
266
|
+
- Built-in smoke suite is available:
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
npm test
|
|
270
|
+
```
|
|
195
271
|
|
|
196
272
|
## License
|
|
197
273
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dax-crafta/auth",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.6",
|
|
4
4
|
"description": "A powerful, flexible, and secure authentication plugin for the Crafta framework. Supports JWT, social login, 2FA, RBAC, audit logging, and enterprise-grade security features.",
|
|
5
5
|
"main": "src/index.js",
|
|
6
6
|
"types": "src/index.d.ts",
|