nest-authme 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 (43) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +305 -0
  3. package/bin/cli.js +11 -0
  4. package/dist/cli.d.ts +2 -0
  5. package/dist/cli.js +1619 -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 +42 -0
  11. package/dist/generator/templates/dto/change-password.dto.ts.hbs +22 -0
  12. package/dist/generator/templates/dto/create-user.dto.ts.hbs +38 -0
  13. package/dist/generator/templates/dto/forgot-password.dto.ts.hbs +13 -0
  14. package/dist/generator/templates/dto/login.dto.ts.hbs +21 -0
  15. package/dist/generator/templates/dto/register.dto.ts.hbs +33 -0
  16. package/dist/generator/templates/dto/reset-password.dto.ts.hbs +22 -0
  17. package/dist/generator/templates/entities/refresh-token.entity.typeorm.hbs +24 -0
  18. package/dist/generator/templates/entities/user.entity.typeorm.hbs +51 -0
  19. package/dist/generator/templates/jwt/auth.controller.ts.hbs +177 -0
  20. package/dist/generator/templates/jwt/auth.module.ts.hbs +81 -0
  21. package/dist/generator/templates/jwt/auth.service.ts.hbs +416 -0
  22. package/dist/generator/templates/jwt/jwt-auth.guard.ts.hbs +24 -0
  23. package/dist/generator/templates/jwt/jwt.strategy.ts.hbs +61 -0
  24. package/dist/generator/templates/jwt/local-auth.guard.ts.hbs +5 -0
  25. package/dist/generator/templates/jwt/local.strategy.ts.hbs +22 -0
  26. package/dist/generator/templates/prisma/prisma.module.ts.hbs +9 -0
  27. package/dist/generator/templates/prisma/prisma.service.ts.hbs +9 -0
  28. package/dist/generator/templates/prisma/schema.prisma.additions.hbs +40 -0
  29. package/dist/generator/templates/rbac/role.enum.ts.hbs +5 -0
  30. package/dist/generator/templates/rbac/roles.guard.ts.hbs +22 -0
  31. package/dist/generator/templates/shared/README.auth.md.hbs +306 -0
  32. package/dist/generator/templates/shared/env.hbs +36 -0
  33. package/dist/generator/templates/shared/env.template.hbs +36 -0
  34. package/dist/generator/templates/shared/main.ts.snippet.hbs +49 -0
  35. package/dist/generator/templates/tests/auth.controller.spec.ts.hbs +189 -0
  36. package/dist/generator/templates/tests/auth.service.spec.ts.hbs +334 -0
  37. package/dist/generator/templates/users/users.controller.ts.hbs +55 -0
  38. package/dist/generator/templates/users/users.module.ts.hbs +31 -0
  39. package/dist/generator/templates/users/users.service.ts.hbs +192 -0
  40. package/dist/index.d.ts +9 -0
  41. package/dist/index.js +1566 -0
  42. package/dist/index.js.map +1 -0
  43. package/package.json +65 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Islam Awad
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,305 @@
1
+ # nest-authme
2
+
3
+ > Add production-ready authentication to any NestJS project in 60 seconds
4
+
5
+ [![npm version](https://badge.fury.io/js/nest-authme.svg)](https://www.npmjs.com/package/nest-authme)
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/nest-authme.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
+ - **JWT Authentication** - Passport.js with access + refresh token rotation
16
+ - **Prisma & TypeORM** - Auto-detects your ORM and generates matching code
17
+ - **RBAC** - Role-based access control with `@Roles()` decorator
18
+ - **Change Password** - Secure password change with current password verification
19
+ - **Forgot / Reset Password** - Token-based password reset flow
20
+ - **Email Verification** - Token-based email verification flow
21
+ - **Username Support** - Optional username field on user entity
22
+ - **Rate Limiting** - `@nestjs/throttler` on auth endpoints, `@SkipThrottle()` on protected routes
23
+ - **Swagger / OpenAPI** - Full API documentation with `@nestjs/swagger` decorators
24
+ - **Unit Tests** - Generated Jest tests for AuthService and AuthController
25
+ - **Custom Decorators** - `@Public()`, `@CurrentUser()`, `@Roles()`
26
+ - **Security Best Practices** - bcrypt, class-validator, secure defaults, crypto-random secrets
27
+ - **`--yes` Flag** - Skip prompts with sensible defaults
28
+
29
+ ---
30
+
31
+ ## Quick Start
32
+
33
+ ```bash
34
+ cd my-nestjs-app
35
+ npx nest-authme
36
+ ```
37
+
38
+ Follow the interactive prompts, or skip them:
39
+
40
+ ```bash
41
+ npx nest-authme --yes
42
+ ```
43
+
44
+ ```
45
+ 🔐 NestJS Authentication Module Generator v1.3.2
46
+
47
+ ✓ Detected NestJS 11.0.1
48
+ ✓ Found TypeORM
49
+ ✓ Source directory: src/
50
+
51
+ ? Choose authentication strategy: JWT Authentication
52
+ ? Enable RBAC? Yes
53
+ ? Select roles: Admin, User
54
+ ? Enable refresh tokens? Yes
55
+ ? JWT Access Token expiration: 1 hour
56
+ ? Refresh Token expiration: 7 days
57
+ ? Enable rate limiting? Yes
58
+ ? Enable Swagger API documentation? Yes
59
+ ? Generate unit tests? Yes
60
+ ? Add username field? No
61
+ ? Enable email verification? No
62
+ ? Enable forgot/reset password? Yes
63
+ ? Auto-install dependencies? Yes
64
+
65
+ 🎉 Success! Authentication module generated.
66
+ ```
67
+
68
+ Then start your app:
69
+
70
+ ```bash
71
+ npm run start:dev
72
+ ```
73
+
74
+ ---
75
+
76
+ ## API Endpoints
77
+
78
+ | Method | Endpoint | Auth | Description |
79
+ |--------|----------|------|-------------|
80
+ | `POST` | `/auth/register` | Public | Register a new user |
81
+ | `POST` | `/auth/login` | Public | Login and get tokens |
82
+ | `POST` | `/auth/change-password` | JWT | Change password |
83
+ | `POST` | `/auth/forgot-password` | Public | Request password reset token |
84
+ | `POST` | `/auth/reset-password` | Public | Reset password with token |
85
+ | `GET` | `/auth/verify-email?token=...` | Public | Verify email address |
86
+ | `POST` | `/auth/resend-verification` | Public | Resend verification token |
87
+ | `POST` | `/auth/refresh` | Public | Refresh access token |
88
+ | `POST` | `/auth/logout` | JWT | Invalidate refresh token |
89
+ | `POST` | `/auth/logout-all` | JWT | Invalidate all refresh tokens |
90
+ | `GET` | `/users/profile` | JWT | Get current user profile |
91
+ | `GET` | `/users` | JWT + Admin | List all users |
92
+
93
+ > Endpoints are conditionally generated based on your selected features.
94
+
95
+ ---
96
+
97
+ ## What Gets Generated
98
+
99
+ ```
100
+ src/
101
+ ├── auth/
102
+ │ ├── auth.module.ts
103
+ │ ├── auth.service.ts
104
+ │ ├── auth.service.spec.ts # (if unit tests enabled)
105
+ │ ├── auth.controller.ts
106
+ │ ├── auth.controller.spec.ts # (if unit tests enabled)
107
+ │ ├── strategies/
108
+ │ │ ├── jwt.strategy.ts
109
+ │ │ └── local.strategy.ts
110
+ │ ├── guards/
111
+ │ │ ├── jwt-auth.guard.ts
112
+ │ │ ├── local-auth.guard.ts
113
+ │ │ └── roles.guard.ts # (if RBAC enabled)
114
+ │ ├── decorators/
115
+ │ │ ├── public.decorator.ts
116
+ │ │ ├── current-user.decorator.ts
117
+ │ │ └── roles.decorator.ts # (if RBAC enabled)
118
+ │ ├── dto/
119
+ │ │ ├── login.dto.ts
120
+ │ │ ├── register.dto.ts
121
+ │ │ ├── change-password.dto.ts
122
+ │ │ ├── forgot-password.dto.ts # (if reset password enabled)
123
+ │ │ ├── reset-password.dto.ts # (if reset password enabled)
124
+ │ │ ├── auth-response.dto.ts
125
+ │ │ └── create-user.dto.ts
126
+ │ ├── enums/
127
+ │ │ └── role.enum.ts # (if RBAC enabled)
128
+ │ └── README.md
129
+ ├── users/
130
+ │ ├── users.module.ts
131
+ │ ├── users.service.ts
132
+ │ ├── users.controller.ts
133
+ │ └── entities/
134
+ │ ├── user.entity.ts # (TypeORM)
135
+ │ └── refresh-token.entity.ts # (TypeORM + refresh tokens)
136
+ ├── prisma/ # (Prisma only)
137
+ │ ├── prisma.service.ts
138
+ │ └── prisma.module.ts
139
+ └── app.module.ts # Updated automatically
140
+
141
+ .env # Auto-generated with secure secret
142
+ .env.example # Git-safe reference
143
+ prisma-schema-additions.prisma # (Prisma only) Models to add
144
+ main.ts.example # Swagger + ValidationPipe setup
145
+ ```
146
+
147
+ ---
148
+
149
+ ## Configuration Options
150
+
151
+ | Prompt | Options | Default |
152
+ |--------|---------|---------|
153
+ | Authentication Strategy | JWT | JWT |
154
+ | Enable RBAC | Yes / No | Yes |
155
+ | Default Roles | Admin, User, Moderator, Guest | Admin, User |
156
+ | Refresh Tokens | Yes / No | Yes |
157
+ | Access Token TTL | 15m, 30m, 1h, 4h, 1d | 1h |
158
+ | Refresh Token TTL | 7d, 30d, 90d, 1y | 7d |
159
+ | Rate Limiting | Yes / No | Yes |
160
+ | Swagger Documentation | Yes / No | Yes |
161
+ | Unit Tests | Yes / No | Yes |
162
+ | Username Field | Yes / No | No |
163
+ | Email Verification | Yes / No | No |
164
+ | Forgot/Reset Password | Yes / No | Yes |
165
+ | Database | PostgreSQL, MySQL, SQLite, MongoDB | Auto-detect |
166
+ | Auto-install | Yes / No | Yes |
167
+
168
+ ---
169
+
170
+ ## ORM Support
171
+
172
+ ### TypeORM (auto-detected)
173
+
174
+ Generates full entity files with decorators. Works with PostgreSQL, MySQL, and SQLite.
175
+
176
+ ### Prisma (auto-detected)
177
+
178
+ Generates a `PrismaService`, `PrismaModule`, and a `prisma-schema-additions.prisma` file containing the models to copy into your `schema.prisma`:
179
+
180
+ ```bash
181
+ # After generation:
182
+ # 1. Copy models from prisma-schema-additions.prisma into prisma/schema.prisma
183
+ # 2. Run migrations:
184
+ npx prisma migrate dev --name add-auth-models
185
+ npx prisma generate
186
+ ```
187
+
188
+ ---
189
+
190
+ ## Usage Examples
191
+
192
+ ### Protect Routes (default behavior)
193
+
194
+ All routes require JWT authentication by default:
195
+
196
+ ```typescript
197
+ @Controller('posts')
198
+ export class PostsController {
199
+ @Get() // Requires JWT token
200
+ findAll() {
201
+ return this.postsService.findAll();
202
+ }
203
+ }
204
+ ```
205
+
206
+ ### Make Routes Public
207
+
208
+ ```typescript
209
+ import { Public } from './auth/decorators/public.decorator';
210
+
211
+ @Public()
212
+ @Get('health')
213
+ healthCheck() {
214
+ return { status: 'ok' };
215
+ }
216
+ ```
217
+
218
+ ### Access Current User
219
+
220
+ ```typescript
221
+ import { CurrentUser } from './auth/decorators/current-user.decorator';
222
+
223
+ @Get('me')
224
+ getProfile(@CurrentUser() user: any) {
225
+ return { id: user.id, email: user.email };
226
+ }
227
+ ```
228
+
229
+ ### Restrict by Role
230
+
231
+ ```typescript
232
+ import { Roles } from './auth/decorators/roles.decorator';
233
+
234
+ @Roles('Admin')
235
+ @Delete(':id')
236
+ deleteUser(@Param('id') id: string) {
237
+ return this.usersService.remove(id);
238
+ }
239
+ ```
240
+
241
+ ---
242
+
243
+ ## Security
244
+
245
+ - Passwords hashed with **bcrypt** (configurable salt rounds via `BCRYPT_ROUNDS`)
246
+ - JWT signed with **HS256** and crypto-random secret
247
+ - Short-lived access tokens (default 1h)
248
+ - One-time use refresh tokens with database storage and rotation
249
+ - Input validation with **class-validator** on all DTOs
250
+ - Rate limiting on auth endpoints (3-5 req/min)
251
+ - Password reset tokens expire after 1 hour
252
+ - Forgot-password endpoint returns generic message to prevent email enumeration
253
+
254
+ ---
255
+
256
+ ## Troubleshooting
257
+
258
+ ### "Not a valid NestJS project"
259
+ Make sure you're in a NestJS project directory with `@nestjs/core` in `package.json`.
260
+
261
+ ### "auth/ directory already exists"
262
+ Delete the existing `src/auth/` directory before running the generator.
263
+
264
+ ### "JWT secret not found"
265
+ The `.env` file is auto-generated. If missing, copy `.env.example` to `.env`.
266
+
267
+ ### "Database connection failed"
268
+ Check your database credentials in `.env` and ensure the database server is running.
269
+
270
+ ---
271
+
272
+ ## Roadmap
273
+
274
+ - OAuth 2.0 (Google, GitHub)
275
+ - Session-based authentication
276
+ - Two-factor authentication (TOTP)
277
+ - Account lockout
278
+ - Admin panel UI
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
+ ## License
292
+
293
+ MIT
294
+
295
+ ---
296
+
297
+ ## Links
298
+
299
+ - **npm**: https://www.npmjs.com/package/nest-authme
300
+ - **GitHub**: https://github.com/Islamawad132/add-nest-auth
301
+ - **Issues**: https://github.com/Islamawad132/add-nest-auth/issues
302
+
303
+ ---
304
+
305
+ **Built 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 { }