add-nest-auth 1.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.
Files changed (33) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +368 -0
  3. package/bin/cli.js +11 -0
  4. package/dist/cli.d.ts +2 -0
  5. package/dist/cli.js +1133 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/generator/templates/decorators/current-user.decorator.ts.hbs +8 -0
  8. package/dist/generator/templates/decorators/public.decorator.ts.hbs +4 -0
  9. package/dist/generator/templates/decorators/roles.decorator.ts.hbs +4 -0
  10. package/dist/generator/templates/dto/auth-response.dto.ts.hbs +13 -0
  11. package/dist/generator/templates/dto/create-user.dto.ts.hbs +17 -0
  12. package/dist/generator/templates/dto/login.dto.ts.hbs +12 -0
  13. package/dist/generator/templates/dto/register.dto.ts.hbs +13 -0
  14. package/dist/generator/templates/entities/refresh-token.entity.typeorm.hbs +24 -0
  15. package/dist/generator/templates/entities/user.entity.typeorm.hbs +30 -0
  16. package/dist/generator/templates/jwt/auth.controller.ts.hbs +34 -0
  17. package/dist/generator/templates/jwt/auth.module.ts.hbs +48 -0
  18. package/dist/generator/templates/jwt/auth.service.ts.hbs +193 -0
  19. package/dist/generator/templates/jwt/jwt-auth.guard.ts.hbs +24 -0
  20. package/dist/generator/templates/jwt/jwt.strategy.ts.hbs +52 -0
  21. package/dist/generator/templates/jwt/local-auth.guard.ts.hbs +5 -0
  22. package/dist/generator/templates/jwt/local.strategy.ts.hbs +22 -0
  23. package/dist/generator/templates/rbac/role.enum.ts.hbs +5 -0
  24. package/dist/generator/templates/rbac/roles.guard.ts.hbs +22 -0
  25. package/dist/generator/templates/shared/README.auth.md.hbs +283 -0
  26. package/dist/generator/templates/shared/env.template.hbs +29 -0
  27. package/dist/generator/templates/users/users.controller.ts.hbs +31 -0
  28. package/dist/generator/templates/users/users.module.ts.hbs +27 -0
  29. package/dist/generator/templates/users/users.service.ts.hbs +93 -0
  30. package/dist/index.d.ts +6 -0
  31. package/dist/index.js +1130 -0
  32. package/dist/index.js.map +1 -0
  33. package/package.json +62 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Your Name
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,368 @@
1
+ # add-nest-auth
2
+
3
+ > Add production-ready authentication to any NestJS project in 60 seconds ⚡
4
+
5
+ [![npm version](https://badge.fury.io/js/add-nest-auth.svg)](https://www.npmjs.com/package/add-nest-auth)
6
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
7
+ [![Node](https://img.shields.io/node/v/add-nest-auth.svg)](https://nodejs.org)
8
+
9
+ Stop writing the same authentication code for every NestJS project. Generate a complete, production-ready auth module with one command.
10
+
11
+ ---
12
+
13
+ ## ✨ Features
14
+
15
+ - 🚀 **60-second setup** - Complete auth module with one command
16
+ - 🔐 **JWT Authentication** - Passport.js + access/refresh tokens
17
+ - 👥 **RBAC Support** - Role-based access control (optional)
18
+ - 🔄 **Token Rotation** - Secure refresh token implementation
19
+ - 🎨 **Custom Decorators** - `@Public()`, `@CurrentUser()`, `@Roles()`
20
+ - 💾 **TypeORM Integration** - Auto-detect and generate entities
21
+ - 🛡️ **Security Best Practices** - bcrypt, class-validator, secure defaults
22
+ - 📦 **Zero Config** - Beautiful interactive CLI
23
+ - 🎯 **Type Safe** - Full TypeScript support
24
+ - ✅ **Production Ready** - Battle-tested patterns
25
+
26
+ ---
27
+
28
+ ## 🚀 Quick Start
29
+
30
+ ### 1. Run the CLI
31
+
32
+ Navigate to your NestJS project and run:
33
+
34
+ ```bash
35
+ cd my-nestjs-app
36
+ npx add-nest-auth
37
+ ```
38
+
39
+ ### 2. Follow Interactive Prompts
40
+
41
+ ```
42
+ 🔐 NestJS Authentication Module Generator v1.0.0
43
+
44
+ ✓ Detected NestJS 11.0.1
45
+ ✓ Source directory: src/
46
+
47
+ ? Choose authentication strategy: JWT Authentication
48
+ ? Enable RBAC? Yes
49
+ ? Select roles: Admin, User
50
+ ? Enable refresh tokens? Yes
51
+ ? JWT expiration: 1 hour
52
+ ? Auto-install dependencies? Yes
53
+
54
+ ⚙️ Generating authentication module...
55
+
56
+ ✓ Generated 21 files
57
+ ✓ Updated app.module.ts
58
+ ✓ Updated package.json
59
+ ✓ Dependencies installed
60
+
61
+ 🎉 Success! Authentication module generated.
62
+ ```
63
+
64
+ ### 3. Configure & Start
65
+
66
+ ```bash
67
+ # Copy environment variables
68
+ cp .env.example .env
69
+
70
+ # Run database migrations (TypeORM)
71
+ npm run migration:generate -- src/migrations/CreateAuthTables
72
+ npm run migration:run
73
+
74
+ # Start your app
75
+ npm run start:dev
76
+ ```
77
+
78
+ ### 4. Test It Out
79
+
80
+ ```bash
81
+ # Register a user
82
+ curl -X POST http://localhost:3000/auth/register \
83
+ -H "Content-Type: application/json" \
84
+ -d '{"email":"user@example.com","password":"Password123!"}'
85
+
86
+ # Login
87
+ curl -X POST http://localhost:3000/auth/login \
88
+ -H "Content-Type: application/json" \
89
+ -d '{"email":"user@example.com","password":"Password123!"}'
90
+
91
+ # Access protected route
92
+ curl http://localhost:3000/users/profile \
93
+ -H "Authorization: Bearer <your-access-token>"
94
+ ```
95
+
96
+ **That's it!** 🎉
97
+
98
+ ---
99
+
100
+ ## 📦 What Gets Generated
101
+
102
+ ### File Structure (21 Files)
103
+
104
+ ```
105
+ src/
106
+ ├── auth/
107
+ │ ├── auth.module.ts # Module configuration
108
+ │ ├── auth.service.ts # Business logic
109
+ │ ├── auth.controller.ts # REST endpoints
110
+ │ ├── strategies/
111
+ │ │ ├── jwt.strategy.ts # JWT validation
112
+ │ │ └── local.strategy.ts # Login validation
113
+ │ ├── guards/
114
+ │ │ ├── jwt-auth.guard.ts # Protect routes
115
+ │ │ ├── local-auth.guard.ts # Login guard
116
+ │ │ └── roles.guard.ts # RBAC guard
117
+ │ ├── decorators/
118
+ │ │ ├── public.decorator.ts # @Public()
119
+ │ │ ├── current-user.decorator.ts # @CurrentUser()
120
+ │ │ └── roles.decorator.ts # @Roles()
121
+ │ ├── dto/
122
+ │ │ ├── login.dto.ts # Login validation
123
+ │ │ ├── register.dto.ts # Register validation
124
+ │ │ ├── auth-response.dto.ts # Response shape
125
+ │ │ └── create-user.dto.ts # User creation
126
+ │ ├── enums/
127
+ │ │ └── role.enum.ts # Role definitions
128
+ │ └── README.md # Usage guide
129
+ ├── users/
130
+ │ ├── users.module.ts
131
+ │ ├── users.service.ts
132
+ │ ├── users.controller.ts
133
+ │ └── entities/
134
+ │ ├── user.entity.ts # User model
135
+ │ └── refresh-token.entity.ts # Refresh tokens
136
+ └── app.module.ts # ✏️ Updated
137
+
138
+ .env.example # Environment template
139
+ package.json # ✏️ Dependencies added
140
+ ```
141
+
142
+ ### Dependencies Added (~8 packages)
143
+
144
+ ```json
145
+ {
146
+ "@nestjs/jwt": "^11.0.0",
147
+ "@nestjs/passport": "^11.0.0",
148
+ "@nestjs/config": "^3.0.0",
149
+ "@nestjs/typeorm": "^11.0.0",
150
+ "passport": "^0.7.0",
151
+ "passport-jwt": "^4.0.1",
152
+ "passport-local": "^1.0.0",
153
+ "bcrypt": "^5.1.1",
154
+ "class-validator": "^0.14.0",
155
+ "class-transformer": "^0.5.1"
156
+ }
157
+ ```
158
+
159
+ ---
160
+
161
+ ## 📖 Usage Examples
162
+
163
+ ### Protect Routes (Default Behavior)
164
+
165
+ All routes require authentication by default:
166
+
167
+ ```typescript
168
+ @Controller('posts')
169
+ export class PostsController {
170
+ @Get() // ⛔ Requires JWT token
171
+ findAll() {
172
+ return this.postsService.findAll();
173
+ }
174
+ }
175
+ ```
176
+
177
+ ### Make Routes Public
178
+
179
+ Use `@Public()` decorator:
180
+
181
+ ```typescript
182
+ import { Public } from './auth/decorators/public.decorator';
183
+
184
+ @Public() // ✅ No authentication needed
185
+ @Get('public')
186
+ getPublicData() {
187
+ return 'Everyone can see this';
188
+ }
189
+ ```
190
+
191
+ ### Access Current User
192
+
193
+ Use `@CurrentUser()` decorator:
194
+
195
+ ```typescript
196
+ import { CurrentUser } from './auth/decorators/current-user.decorator';
197
+
198
+ @Get('me')
199
+ getProfile(@CurrentUser() user: any) {
200
+ return {
201
+ id: user.id,
202
+ email: user.email,
203
+ roles: user.roles,
204
+ };
205
+ }
206
+ ```
207
+
208
+ ### Restrict by Role (RBAC)
209
+
210
+ Use `@Roles()` decorator:
211
+
212
+ ```typescript
213
+ import { Roles } from './auth/decorators/roles.decorator';
214
+ import { RolesGuard } from './auth/guards/roles.guard';
215
+
216
+ @UseGuards(JwtAuthGuard, RolesGuard)
217
+ @Roles('Admin')
218
+ @Delete(':id')
219
+ deleteUser(@Param('id') id: string) {
220
+ return this.usersService.remove(id);
221
+ }
222
+ ```
223
+
224
+ ### Refresh Tokens
225
+
226
+ Automatically generated endpoint:
227
+
228
+ ```bash
229
+ POST /auth/refresh
230
+ Body: { "refreshToken": "..." }
231
+ Response: { "accessToken": "..." }
232
+ ```
233
+
234
+ ---
235
+
236
+ ## 🎯 Configuration Options
237
+
238
+ ### Interactive Prompts
239
+
240
+ | Prompt | Options | Default |
241
+ |--------|---------|---------|
242
+ | **Authentication Strategy** | JWT, OAuth (v1.1), Session (v1.2) | JWT |
243
+ | **Enable RBAC** | Yes, No | Yes |
244
+ | **Default Roles** | Admin, User, Moderator, Guest | Admin, User |
245
+ | **Refresh Tokens** | Yes, No | Yes |
246
+ | **Access Token TTL** | 15m, 30m, 1h, 4h, 1d | 1h |
247
+ | **Refresh Token TTL** | 7d, 30d, 90d, 1y | 7d |
248
+ | **Database** | PostgreSQL, MySQL, SQLite, MongoDB | Auto-detect |
249
+ | **Auto-install** | Yes, No | Yes |
250
+
251
+ ### Command-Line Flags (Coming Soon)
252
+
253
+ ```bash
254
+ npx add-nest-auth --preset jwt-rbac # Use preset
255
+ npx add-nest-auth --dry-run # Preview changes
256
+ npx add-nest-auth --force # Overwrite existing
257
+ npx add-nest-auth --no-install # Skip npm install
258
+ ```
259
+
260
+ ---
261
+
262
+ ## 🔒 Security Features
263
+
264
+ - ✅ **Password Hashing** - bcrypt with salt rounds
265
+ - ✅ **JWT Signing** - HS256 algorithm with secrets
266
+ - ✅ **Token Expiration** - Short-lived access tokens
267
+ - ✅ **Refresh Rotation** - One-time use refresh tokens
268
+ - ✅ **Input Validation** - class-validator on all DTOs
269
+ - ✅ **Type Safety** - Full TypeScript coverage
270
+ - ✅ **Guard Protection** - Automatic route protection
271
+
272
+ ---
273
+
274
+ ## 📚 Documentation
275
+
276
+ - **[Complete Usage Guide](./USAGE.md)** - Comprehensive documentation
277
+ - **[Generated README](./src/auth/README.md)** - Created after generation
278
+ - **[NestJS Docs](https://docs.nestjs.com/security/authentication)** - Official docs
279
+
280
+ ---
281
+
282
+ ## 🛠️ Requirements
283
+
284
+ - **Node.js** >= 18.0.0
285
+ - **NestJS** >= 10.0.0
286
+ - **TypeScript** >= 5.0.0
287
+ - **Package Manager**: npm, yarn, or pnpm
288
+
289
+ ---
290
+
291
+ ## 🎬 Coming Soon
292
+
293
+ ### v1.1 - OAuth Integration
294
+ - Google OAuth
295
+ - GitHub OAuth
296
+ - Facebook OAuth
297
+
298
+ ### v1.2 - Session-Based Auth
299
+ - Express session support
300
+ - Cookie-based authentication
301
+
302
+ ### v1.3 - Multi-ORM Support
303
+ - Prisma templates
304
+ - Mongoose templates
305
+
306
+ ### v1.4 - Advanced Features
307
+ - Email verification
308
+ - Password reset flow
309
+ - Two-factor authentication (TOTP)
310
+ - Account lockout
311
+
312
+ ### v2.0 - Admin Panel
313
+ - Auto-generated admin UI
314
+ - User management
315
+ - Role management
316
+
317
+ ---
318
+
319
+ ## 🐛 Troubleshooting
320
+
321
+ ### "Not a valid NestJS project"
322
+ Ensure you're in a NestJS project directory with `@nestjs/core` in package.json.
323
+
324
+ ### "auth/ directory already exists"
325
+ Delete existing `src/auth/` directory or use `--force` flag (coming soon).
326
+
327
+ ### "JWT secret not found"
328
+ Copy `.env.example` to `.env` and set `JWT_SECRET`.
329
+
330
+ ### "Database connection failed"
331
+ Check database credentials in `.env` and ensure database is running.
332
+
333
+ **[See full troubleshooting guide →](./USAGE.md#troubleshooting)**
334
+
335
+ ---
336
+
337
+ ## 🤝 Contributing
338
+
339
+ Contributions are welcome! Please see [CONTRIBUTING.md](./CONTRIBUTING.md) for details.
340
+
341
+ ---
342
+
343
+ ## 📄 License
344
+
345
+ MIT © [Your Name]
346
+
347
+ ---
348
+
349
+ ## 🌟 Show Your Support
350
+
351
+ If this tool helped you, please consider:
352
+
353
+ - ⭐ Starring the repo
354
+ - 🐛 Reporting issues
355
+ - 💡 Suggesting features
356
+ - 📢 Sharing with others
357
+
358
+ ---
359
+
360
+ ## 🔗 Links
361
+
362
+ - **GitHub**: https://github.com/yourusername/add-nest-auth
363
+ - **npm**: https://www.npmjs.com/package/add-nest-auth
364
+ - **Issues**: https://github.com/yourusername/add-nest-auth/issues
365
+
366
+ ---
367
+
368
+ **Built with ❤️ for the NestJS community**
package/bin/cli.js ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * CLI executable wrapper
5
+ * This file is used when running via npm/npx
6
+ */
7
+
8
+ import('../dist/cli.js').catch((error) => {
9
+ console.error('Failed to load CLI:', error);
10
+ process.exit(1);
11
+ });
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { }