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.
- package/LICENSE +21 -0
- package/README.md +305 -0
- package/bin/cli.js +11 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +1619 -0
- package/dist/cli.js.map +1 -0
- package/dist/generator/templates/decorators/current-user.decorator.ts.hbs +8 -0
- package/dist/generator/templates/decorators/public.decorator.ts.hbs +4 -0
- package/dist/generator/templates/decorators/roles.decorator.ts.hbs +4 -0
- package/dist/generator/templates/dto/auth-response.dto.ts.hbs +42 -0
- package/dist/generator/templates/dto/change-password.dto.ts.hbs +22 -0
- package/dist/generator/templates/dto/create-user.dto.ts.hbs +38 -0
- package/dist/generator/templates/dto/forgot-password.dto.ts.hbs +13 -0
- package/dist/generator/templates/dto/login.dto.ts.hbs +21 -0
- package/dist/generator/templates/dto/register.dto.ts.hbs +33 -0
- package/dist/generator/templates/dto/reset-password.dto.ts.hbs +22 -0
- package/dist/generator/templates/entities/refresh-token.entity.typeorm.hbs +24 -0
- package/dist/generator/templates/entities/user.entity.typeorm.hbs +51 -0
- package/dist/generator/templates/jwt/auth.controller.ts.hbs +177 -0
- package/dist/generator/templates/jwt/auth.module.ts.hbs +81 -0
- package/dist/generator/templates/jwt/auth.service.ts.hbs +416 -0
- package/dist/generator/templates/jwt/jwt-auth.guard.ts.hbs +24 -0
- package/dist/generator/templates/jwt/jwt.strategy.ts.hbs +61 -0
- package/dist/generator/templates/jwt/local-auth.guard.ts.hbs +5 -0
- package/dist/generator/templates/jwt/local.strategy.ts.hbs +22 -0
- package/dist/generator/templates/prisma/prisma.module.ts.hbs +9 -0
- package/dist/generator/templates/prisma/prisma.service.ts.hbs +9 -0
- package/dist/generator/templates/prisma/schema.prisma.additions.hbs +40 -0
- package/dist/generator/templates/rbac/role.enum.ts.hbs +5 -0
- package/dist/generator/templates/rbac/roles.guard.ts.hbs +22 -0
- package/dist/generator/templates/shared/README.auth.md.hbs +306 -0
- package/dist/generator/templates/shared/env.hbs +36 -0
- package/dist/generator/templates/shared/env.template.hbs +36 -0
- package/dist/generator/templates/shared/main.ts.snippet.hbs +49 -0
- package/dist/generator/templates/tests/auth.controller.spec.ts.hbs +189 -0
- package/dist/generator/templates/tests/auth.service.spec.ts.hbs +334 -0
- package/dist/generator/templates/users/users.controller.ts.hbs +55 -0
- package/dist/generator/templates/users/users.module.ts.hbs +31 -0
- package/dist/generator/templates/users/users.service.ts.hbs +192 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +1566 -0
- package/dist/index.js.map +1 -0
- package/package.json +65 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { Injectable, ConflictException, NotFoundException } from '@nestjs/common';
|
|
2
|
+
{{#if (eq orm "typeorm")}}
|
|
3
|
+
import { InjectRepository } from '@nestjs/typeorm';
|
|
4
|
+
import { Repository } from 'typeorm';
|
|
5
|
+
import { User } from './entities/user.entity';
|
|
6
|
+
{{else if (eq orm "prisma")}}
|
|
7
|
+
import { PrismaService } from '../prisma/prisma.service';
|
|
8
|
+
{{/if}}
|
|
9
|
+
import { CreateUserDto } from '../auth/dto/create-user.dto';
|
|
10
|
+
|
|
11
|
+
@Injectable()
|
|
12
|
+
export class UsersService {
|
|
13
|
+
{{#if (eq orm "typeorm")}}
|
|
14
|
+
constructor(
|
|
15
|
+
@InjectRepository(User)
|
|
16
|
+
private usersRepository: Repository<User>,
|
|
17
|
+
) {}
|
|
18
|
+
|
|
19
|
+
async create(createUserDto: CreateUserDto): Promise<User> {
|
|
20
|
+
const existingUser = await this.usersRepository.findOne({
|
|
21
|
+
where: { email: createUserDto.email },
|
|
22
|
+
});
|
|
23
|
+
if (existingUser) {
|
|
24
|
+
throw new ConflictException('User with this email already exists');
|
|
25
|
+
}
|
|
26
|
+
const user = this.usersRepository.create(createUserDto);
|
|
27
|
+
return await this.usersRepository.save(user);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async findByEmail(email: string): Promise<User | null> {
|
|
31
|
+
return await this.usersRepository.findOne({ where: { email } });
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async findById(id: string): Promise<User | null> {
|
|
35
|
+
return await this.usersRepository.findOne({ where: { id } });
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async findAll(): Promise<User[]> {
|
|
39
|
+
return await this.usersRepository.find({
|
|
40
|
+
select: ['id', 'email'{{#if features.useUsername}}, 'username'{{/if}}{{#if rbac.enabled}}, 'roles'{{/if}}, 'createdAt'],
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async updatePassword(id: string, hashedPassword: string): Promise<void> {
|
|
45
|
+
await this.usersRepository.update(id, { password: hashedPassword });
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
{{#if features.emailVerification}}
|
|
49
|
+
async setVerificationToken(id: string, token: string): Promise<void> {
|
|
50
|
+
await this.usersRepository.update(id, { emailVerificationToken: token });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async findByVerificationToken(token: string): Promise<User | null> {
|
|
54
|
+
return await this.usersRepository.findOne({ where: { emailVerificationToken: token } });
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async markEmailVerified(id: string): Promise<void> {
|
|
58
|
+
await this.usersRepository.update(id, { isEmailVerified: true, emailVerificationToken: null as any });
|
|
59
|
+
}
|
|
60
|
+
{{/if}}
|
|
61
|
+
|
|
62
|
+
{{#if features.resetPassword}}
|
|
63
|
+
async setResetToken(id: string, token: string, expires: Date): Promise<void> {
|
|
64
|
+
await this.usersRepository.update(id, { passwordResetToken: token, passwordResetExpires: expires });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async findByResetToken(token: string): Promise<User | null> {
|
|
68
|
+
return await this.usersRepository.findOne({ where: { passwordResetToken: token } });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async clearResetToken(id: string): Promise<void> {
|
|
72
|
+
await this.usersRepository.update(id, { passwordResetToken: null as any, passwordResetExpires: null as any });
|
|
73
|
+
}
|
|
74
|
+
{{/if}}
|
|
75
|
+
{{else if (eq orm "prisma")}}
|
|
76
|
+
constructor(private prisma: PrismaService) {}
|
|
77
|
+
|
|
78
|
+
async create(createUserDto: CreateUserDto): Promise<any> {
|
|
79
|
+
const existingUser = await this.prisma.user.findUnique({
|
|
80
|
+
where: { email: createUserDto.email },
|
|
81
|
+
});
|
|
82
|
+
if (existingUser) {
|
|
83
|
+
throw new ConflictException('User with this email already exists');
|
|
84
|
+
}
|
|
85
|
+
return await this.prisma.user.create({ data: createUserDto });
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async findByEmail(email: string): Promise<any> {
|
|
89
|
+
return await this.prisma.user.findUnique({ where: { email } });
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async findById(id: string): Promise<any> {
|
|
93
|
+
return await this.prisma.user.findUnique({ where: { id } });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async findAll(): Promise<any[]> {
|
|
97
|
+
return await this.prisma.user.findMany({
|
|
98
|
+
select: {
|
|
99
|
+
id: true,
|
|
100
|
+
email: true,
|
|
101
|
+
{{#if features.useUsername}}
|
|
102
|
+
username: true,
|
|
103
|
+
{{/if}}
|
|
104
|
+
{{#if rbac.enabled}}
|
|
105
|
+
roles: true,
|
|
106
|
+
{{/if}}
|
|
107
|
+
createdAt: true,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async updatePassword(id: string, hashedPassword: string): Promise<void> {
|
|
113
|
+
await this.prisma.user.update({ where: { id }, data: { password: hashedPassword } });
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
{{#if features.emailVerification}}
|
|
117
|
+
async setVerificationToken(id: string, token: string): Promise<void> {
|
|
118
|
+
await this.prisma.user.update({ where: { id }, data: { emailVerificationToken: token } });
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async findByVerificationToken(token: string): Promise<any> {
|
|
122
|
+
return await this.prisma.user.findFirst({ where: { emailVerificationToken: token } });
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async markEmailVerified(id: string): Promise<void> {
|
|
126
|
+
await this.prisma.user.update({ where: { id }, data: { isEmailVerified: true, emailVerificationToken: null } });
|
|
127
|
+
}
|
|
128
|
+
{{/if}}
|
|
129
|
+
|
|
130
|
+
{{#if features.resetPassword}}
|
|
131
|
+
async setResetToken(id: string, token: string, expires: Date): Promise<void> {
|
|
132
|
+
await this.prisma.user.update({ where: { id }, data: { passwordResetToken: token, passwordResetExpires: expires } });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
async findByResetToken(token: string): Promise<any> {
|
|
136
|
+
return await this.prisma.user.findFirst({ where: { passwordResetToken: token } });
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async clearResetToken(id: string): Promise<void> {
|
|
140
|
+
await this.prisma.user.update({ where: { id }, data: { passwordResetToken: null, passwordResetExpires: null } });
|
|
141
|
+
}
|
|
142
|
+
{{/if}}
|
|
143
|
+
{{else}}
|
|
144
|
+
async create(createUserDto: CreateUserDto): Promise<any> {
|
|
145
|
+
throw new Error('Not implemented - implement with your ORM');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
async findByEmail(email: string): Promise<any> {
|
|
149
|
+
throw new Error('Not implemented');
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async findById(id: string): Promise<any> {
|
|
153
|
+
throw new Error('Not implemented');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async findAll(): Promise<any[]> {
|
|
157
|
+
throw new Error('Not implemented');
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async updatePassword(id: string, hashedPassword: string): Promise<void> {
|
|
161
|
+
throw new Error('Not implemented');
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
{{#if features.emailVerification}}
|
|
165
|
+
async setVerificationToken(id: string, token: string): Promise<void> {
|
|
166
|
+
throw new Error('Not implemented');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
async findByVerificationToken(token: string): Promise<any> {
|
|
170
|
+
throw new Error('Not implemented');
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async markEmailVerified(id: string): Promise<void> {
|
|
174
|
+
throw new Error('Not implemented');
|
|
175
|
+
}
|
|
176
|
+
{{/if}}
|
|
177
|
+
|
|
178
|
+
{{#if features.resetPassword}}
|
|
179
|
+
async setResetToken(id: string, token: string, expires: Date): Promise<void> {
|
|
180
|
+
throw new Error('Not implemented');
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async findByResetToken(token: string): Promise<any> {
|
|
184
|
+
throw new Error('Not implemented');
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async clearResetToken(id: string): Promise<void> {
|
|
188
|
+
throw new Error('Not implemented');
|
|
189
|
+
}
|
|
190
|
+
{{/if}}
|
|
191
|
+
{{/if}}
|
|
192
|
+
}
|