@dax-crafta/auth 2.1.6 → 2.1.8

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/.mocharc.cjs ADDED
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ spec: 'test/*.test.js',
3
+ timeout: 30000,
4
+ reporter: 'spec',
5
+ require: [],
6
+ };
package/README.md CHANGED
@@ -1,274 +1,167 @@
1
- # @dax-crafta/auth
2
-
3
- Production-ready auth for Crafta apps with plug-and-play defaults and clean JSON customization.
4
-
5
- [![npm version](https://img.shields.io/npm/v/@dax-crafta/auth.svg)](https://www.npmjs.com/package/@dax-crafta/auth)
6
- [![License](https://img.shields.io/npm/l/@dax-crafta/auth.svg)](https://github.com/daxp472/crafta/blob/main/LICENSE)
7
- [![Downloads](https://img.shields.io/npm/dm/@dax-crafta/auth.svg)](https://www.npmjs.com/package/@dax-crafta/auth)
8
-
9
- ## Why this package
10
-
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.
14
-
15
- ## What you get
16
-
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)
25
-
26
- ## 1. Install
27
-
28
- ```bash
29
- npm install crafta @dax-crafta/auth
30
- ```
31
-
32
- ## 2. Minimal setup (copy-paste)
33
-
34
- Create `server.js`:
35
-
36
- ```js
37
- const { crafta } = require('crafta');
38
- const { auth } = require('@dax-crafta/auth');
39
-
40
- const app = crafta();
41
-
42
- auth({
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
- }
54
- })(app);
55
-
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);
155
- ```
156
-
157
- ## 5. Feature toggles (fast on/off)
158
-
159
- Set any feature to `false` and it turns off.
160
-
161
- ```js
162
- auth({
163
- features: {
164
- emailVerification: false,
165
- loginAlerts: false,
166
- securityAttempts: true,
167
- rateLimit: true,
168
- auditLogs: true,
169
- twoFactor: true,
170
- csrf: false
171
- }
172
- })(app);
173
- ```
174
-
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)
180
-
181
- Enable this only when SMTP is ready:
182
-
183
- ```js
184
- auth({
185
- features: {
186
- emailVerification: true,
187
- loginAlerts: true
188
- },
189
- smtp: {
190
- host: process.env.SMTP_HOST,
191
- port: Number(process.env.SMTP_PORT || 587),
192
- auth: {
193
- user: process.env.SMTP_USER,
194
- pass: process.env.SMTP_PASS
195
- },
196
- from: process.env.SMTP_FROM
197
- },
198
- baseUrl: process.env.BASE_URL || 'http://localhost:3000'
199
- })(app);
200
- ```
201
-
202
- ## 7. Google OAuth setup (optional)
203
-
204
- ```js
205
- auth({
206
- social: {
207
- google: {
208
- clientID: process.env.GOOGLE_CLIENT_ID,
209
- clientSecret: process.env.GOOGLE_CLIENT_SECRET,
210
- callbackURL: 'http://localhost:3000/auth/google/callback'
211
- }
212
- }
213
- })(app);
214
- ```
215
-
216
- Routes:
217
- - `GET /auth/google`
218
- - `GET /auth/google/callback`
219
-
220
- ## 8. RBAC quick use
221
-
222
- ```js
223
- const role = await app.roleService.createRole({
224
- name: 'admin',
225
- permissions: [
226
- { resource: 'users', actions: ['create', 'read', 'update', 'delete'] }
227
- ]
228
- });
229
-
230
- const allowed = await app.roleService.checkPermission('admin', 'users', 'delete');
231
- ```
232
-
233
- ## 9. Environment variables
234
-
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=...
246
- ```
247
-
248
- ## 10. Troubleshooting
249
-
250
- - 401 on protected route
251
- - Token missing or expired.
252
- - Send `Authorization: Bearer <accessToken>`.
253
-
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.
260
-
261
- - Refresh token invalid
262
- - Refresh token is rotated; use latest token returned by API.
263
-
264
- ## 11. Release confidence
265
-
266
- - Built-in smoke suite is available:
267
-
268
- ```bash
269
- npm test
270
- ```
271
-
272
- ## License
273
-
274
- MIT © Dax Crafta
1
+ # @dax-crafta/auth
2
+
3
+ Production-ready authentication library for Crafta apps, featuring plug-and-play defaults, robust security, and clean JSON-based customization.
4
+
5
+ [![npm version](https://img.shields.io/npm/v/@dax-crafta/auth.svg)](https://www.npmjs.com/package/@dax-crafta/auth)
6
+ [![License](https://img.shields.io/npm/l/@dax-crafta/auth.svg)](https://github.com/NPM-crafta/crafta-auth/blob/main/LICENSE)
7
+
8
+ ## Features
9
+ 1. **JWT Authentication**: Secure sessions with automatic refresh token rotation.
10
+ 2. **Standard Routes**: Built-in register, login, verify email, forgot/reset password, and refresh routes.
11
+ 3. **Multi-Factor Authentication (MFA)**: Optional TOTP-based 2FA with backup codes support.
12
+ 4. **RBAC**: Role-Based Access Control service to manage permissions and roles.
13
+ 5. **Rate Limiting**: Route-level protection for sensitive endpoints (login, register, 2FA).
14
+ 6. **Password Policy**: Strict policy enforcement with password history checks and strength validation.
15
+ 7. **Google OAuth**: Optional social login integration.
16
+ 8. **Audit Logs**: Comprehensive logging to MongoDB and local files.
17
+ 9. **Session Management**: Full session control with per-user session limits and global logout.
18
+ 10. **Webhooks**: Real-time event notifications for auth lifecycle events (registered, login, logout, password change).
19
+ 11. **Passwordless Login**: Magic link authentication.
20
+ 12. **Standardized Error Handling**: Unified error format with 19 distinct `AUTH_*` codes.
21
+ 13. **CORS Middleware**: Explicit origin whitelisting for secure cross-origin requests.
22
+ 14. **Customizable Fields**: Flexible user model customization.
23
+
24
+ ## Install
25
+ ```bash
26
+ npm install @dax-crafta/auth
27
+ ```
28
+
29
+ ## Minimal Setup
30
+ ```javascript
31
+ const { crafta } = require('crafta');
32
+ const { auth } = require('@dax-crafta/auth');
33
+
34
+ const app = crafta();
35
+
36
+ auth({
37
+ env: {
38
+ JWT_SECRET: process.env.JWT_SECRET || 'dev-secret-123'
39
+ },
40
+ mongoUrl: process.env.MONGO_URL || 'mongodb://127.0.0.1:27017/crafta-auth-demo'
41
+ })(app);
42
+
43
+ app.listen(3000, () => console.log('Server running on http://localhost:3000'));
44
+ ```
45
+
46
+ ## Configuration Options
47
+ The library is configured via a single object passed to the `auth()` function:
48
+
49
+ ```javascript
50
+ auth({
51
+ strategy: 'jwt', // Currently defaults to 'jwt'
52
+ fields: ['name', 'email', 'password'],
53
+ routes: { /* Custom route paths */ },
54
+ passwordPolicy: {
55
+ minLength: 8,
56
+ requireUppercase: true,
57
+ requireNumbers: true,
58
+ requireSpecialChars: true,
59
+ expiryDays: 90
60
+ },
61
+ limits: {
62
+ loginMax: 10,
63
+ registerMax: 20
64
+ }
65
+ })(app);
66
+ ```
67
+
68
+ ## Feature Toggles
69
+ Enable or disable features via the `features` object:
70
+
71
+ ```javascript
72
+ auth({
73
+ features: {
74
+ emailVerification: true,
75
+ twoFactor: true,
76
+ passwordless: false,
77
+ auditLogs: true,
78
+ webhooks: false,
79
+ rateLimit: true,
80
+ csrf: false
81
+ }
82
+ })(app);
83
+ ```
84
+
85
+ ## Email Setup
86
+ Configure SMTP to enable email-based features (Verification, Forgot Password, Magic Links, Login Alerts):
87
+
88
+ ```javascript
89
+ auth({
90
+ smtp: {
91
+ host: process.env.SMTP_HOST,
92
+ port: 587,
93
+ auth: {
94
+ user: process.env.SMTP_USER,
95
+ pass: process.env.SMTP_PASS
96
+ },
97
+ from: process.env.SMTP_FROM
98
+ },
99
+ baseUrl: 'http://localhost:3000'
100
+ })(app);
101
+ ```
102
+
103
+ ## Google OAuth Setup
104
+ ```javascript
105
+ auth({
106
+ social: {
107
+ google: {
108
+ clientID: process.env.GOOGLE_CLIENT_ID,
109
+ clientSecret: process.env.GOOGLE_CLIENT_SECRET,
110
+ callbackURL: 'http://localhost:3000/auth/google/callback'
111
+ }
112
+ }
113
+ })(app);
114
+ ```
115
+
116
+ ## RBAC Quick Use
117
+ Access the role service directly from your app instance:
118
+
119
+ ```javascript
120
+ // Check permission
121
+ const allowed = await app.roleService.checkPermission('admin', 'users', 'delete');
122
+
123
+ // Create role
124
+ await app.roleService.createRole({
125
+ name: 'editor',
126
+ permissions: [{ resource: 'posts', actions: ['create', 'update'] }]
127
+ });
128
+ ```
129
+
130
+ ## Environment Variables
131
+ - `JWT_SECRET`: Secret key for signing tokens.
132
+ - `MONGO_URL`: MongoDB connection string.
133
+ - `SMTP_HOST`, `SMTP_PORT`, `SMTP_USER`, `SMTP_PASS`, `SMTP_FROM`: SMTP configuration.
134
+ - `BASE_URL`: Base URL for email links.
135
+ - `GOOGLE_CLIENT_ID`, `GOOGLE_CLIENT_SECRET`: Google OAuth credentials.
136
+
137
+ ## Troubleshooting
138
+ - **401 Unauthorized**: Check if `Authorization: Bearer <accessToken>` header is present and valid.
139
+ - **Password policy errors**: Ensure passwords meet length, uppercase, number, and special character requirements.
140
+ - **Email issues**: Verify SMTP configuration; features relying on email will automatically disable if SMTP is misconfigured.
141
+
142
+ ## Error Codes
143
+ | Code | Description | HTTP Status |
144
+ | :--- | :--- | :--- |
145
+ | `AUTH_BAD_REQUEST` | Generic bad request error | 400 |
146
+ | `AUTH_VALIDATION` | Invalid request data | 400 |
147
+ | `AUTH_PASSWORD_POLICY` | Password policy violation | 400 |
148
+ | `AUTH_PASSWORD_REUSE` | Cannot reuse previous passwords | 400 |
149
+ | `AUTH_SOCIAL_FAILED` | Social login failure | 400 |
150
+ | `AUTH_INVALID_ID` | Invalid resource identifier | 400 |
151
+ | `AUTH_UNAUTHORIZED` | Authentication required | 401 |
152
+ | `AUTH_INVALID_CREDENTIALS` | Invalid email or password | 401 |
153
+ | `AUTH_INVALID_TOKEN` | Token invalid or missing | 401 |
154
+ | `AUTH_EXPIRED_TOKEN` | Token expired | 401 |
155
+ | `AUTH_2FA_FAILED` | Invalid 2FA code | 401 |
156
+ | `AUTH_FORBIDDEN` | Insufficient permissions | 403 |
157
+ | `AUTH_ACCOUNT_LOCKED` | Account locked due to failed attempts | 403 |
158
+ | `AUTH_EMAIL_NOT_VERIFIED` | Email verification required | 403 |
159
+ | `AUTH_2FA_REQUIRED` | 2FA code input required | 403 |
160
+ | `AUTH_NOT_FOUND` | Resource not found | 404 |
161
+ | `AUTH_CONFLICT` | Resource already exists | 409 |
162
+ | `AUTH_RATE_LIMITED` | Too many requests | 429 |
163
+ | `AUTH_SMTP_DISABLED` | Email delivery service disabled | 503 |
164
+ | `AUTH_INTERNAL` | Unexpected internal error | 500 |
165
+
166
+ ## License
167
+ MIT © Dax Crafta
package/image.png ADDED
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dax-crafta/auth",
3
- "version": "2.1.6",
3
+ "version": "2.1.8",
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",
@@ -61,5 +61,9 @@
61
61
  },
62
62
  "homepage": "https://github.com/daxp472/crafta#readme",
63
63
  "license": "MIT",
64
- "author": "Dax Patel"
64
+ "author": "Dax Patel",
65
+ "devDependencies": {
66
+ "@types/mocha": "^10.0.10",
67
+ "mocha": "^10.8.2"
68
+ }
65
69
  }