@hedhog/admin 0.46.39 → 0.46.41
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 +960 -960
- package/dist/auth/auth.service.d.ts.map +1 -1
- package/dist/auth/auth.service.js +6 -4
- package/dist/auth/auth.service.js.map +1 -1
- package/dist/auth/consts/body.js +24 -24
- package/dist/emails/templates.d.ts.map +1 -1
- package/dist/emails/templates.js +48 -48
- package/dist/emails/templates.js.map +1 -1
- package/frontend/menu/components/create-panel.tsx.ejs +55 -55
- package/frontend/menu/components/update-panel.tsx.ejs +67 -67
- package/frontend/menu/locales/en/admin.menu.json +11 -11
- package/frontend/menu/locales/pt/admin.menu.json +11 -11
- package/frontend/menu/react-query/handlers.ts.ejs +28 -28
- package/frontend/menu/react-query/requests.ts.ejs +56 -56
- package/frontend/menu-locale/locales/en/admin.menu-locale.json +11 -11
- package/frontend/menu-locale/locales/pt/admin.menu-locale.json +11 -11
- package/frontend/menu-screen/locales/en/admin.menu-screen.json +11 -11
- package/frontend/menu-screen/locales/pt/admin.menu-screen.json +11 -11
- package/frontend/multifactor/components/create-panel.tsx.ejs +55 -55
- package/frontend/multifactor/components/update-panel.tsx.ejs +70 -70
- package/frontend/multifactor/locales/en/admin.multifactor.json +11 -11
- package/frontend/multifactor/locales/pt/admin.multifactor.json +11 -11
- package/frontend/multifactor/react-query/handlers.ts.ejs +28 -28
- package/frontend/multifactor/react-query/requests.ts.ejs +59 -59
- package/frontend/multifactor-locale/locales/en/admin.multifactor-locale.json +11 -11
- package/frontend/multifactor-locale/locales/pt/admin.multifactor-locale.json +11 -11
- package/frontend/screen/components/create-panel.tsx.ejs +55 -55
- package/frontend/screen/components/update-panel.tsx.ejs +67 -67
- package/frontend/screen/locales/en/admin.screen.json +11 -11
- package/frontend/screen/locales/pt/admin.screen.json +11 -11
- package/frontend/screen/react-query/handlers.ts.ejs +28 -28
- package/frontend/screen/react-query/requests.ts.ejs +56 -56
- package/frontend/screen-locale/locales/en/admin.screen-locale.json +11 -11
- package/frontend/screen-locale/locales/pt/admin.screen-locale.json +11 -11
- package/frontend/translation/components/create-panel.tsx.ejs +52 -52
- package/frontend/translation/components/update-panel.tsx.ejs +67 -67
- package/frontend/translation/locales/en/admin.translation.json +11 -11
- package/frontend/translation/locales/pt/admin.translation.json +11 -11
- package/frontend/translation/react-query/handlers.ts.ejs +28 -28
- package/frontend/translation/react-query/requests.ts.ejs +58 -58
- package/frontend/translation-namespace/components/create-panel.tsx.ejs +53 -53
- package/frontend/translation-namespace/components/update-panel.tsx.ejs +70 -70
- package/frontend/translation-namespace/locales/en/admin.translation-namespace.json +11 -11
- package/frontend/translation-namespace/locales/pt/admin.translation-namespace.json +11 -11
- package/frontend/translation-namespace/react-query/handlers.ts.ejs +28 -28
- package/frontend/translation-namespace/react-query/requests.ts.ejs +60 -60
- package/frontend/user/components/create-panel.tsx.ejs +52 -52
- package/frontend/user/components/update-panel.tsx.ejs +64 -64
- package/frontend/user/locales/en/admin.user.json +11 -11
- package/frontend/user/locales/pt/admin.user.json +11 -11
- package/frontend/user/react-query/handlers.ts.ejs +28 -28
- package/frontend/user/react-query/requests.ts.ejs +55 -55
- package/hedhog.yaml +783 -783
- package/package.json +45 -45
- package/src/admin.module.ts +39 -39
- package/src/auth/auth.controller.ts +88 -88
- package/src/auth/auth.module.ts +41 -41
- package/src/auth/auth.service.spec.ts +196 -196
- package/src/auth/auth.service.ts +355 -349
- package/src/auth/consts/body.ts +27 -27
- package/src/auth/dto/change.dto.ts +19 -19
- package/src/auth/dto/email.dto.ts +15 -15
- package/src/auth/dto/forget.dto.ts +6 -6
- package/src/auth/dto/login.dto.ts +21 -21
- package/src/auth/dto/otp.dto.ts +11 -11
- package/src/auth/dto/reset.dto.ts +14 -14
- package/src/auth/enums/multifactor-type.enum.ts +4 -4
- package/src/auth/guards/auth.guard.ts +54 -54
- package/src/auth/types/user.type.ts +8 -8
- package/src/dto/delete.dto.ts +8 -8
- package/src/dto/update-ids.dto.ts +9 -9
- package/src/emails/index.ts +2 -2
- package/src/emails/lib.ts +40 -40
- package/src/emails/templates.ts +62 -60
- package/src/index.ts +20 -20
- package/src/menu/dto/create.dto.ts +25 -25
- package/src/menu/dto/order.dto.ts +8 -8
- package/src/menu/dto/update.dto.ts +19 -19
- package/src/menu/menu.controller.ts +105 -105
- package/src/menu/menu.module.ts +18 -18
- package/src/menu/menu.service.spec.ts +247 -247
- package/src/menu/menu.service.ts +263 -263
- package/src/role/dto/create.dto.ts +7 -7
- package/src/role/dto/update.dto.ts +4 -4
- package/src/role/guards/role.guard.ts +121 -121
- package/src/role/role.controller.ts +126 -126
- package/src/role/role.module.ts +28 -28
- package/src/role/role.service.spec.ts +417 -417
- package/src/role/role.service.ts +289 -289
- package/src/route/dto/create.dto.ts +13 -13
- package/src/route/dto/update.dto.ts +15 -15
- package/src/route/route.controller.ts +91 -91
- package/src/route/route.module.ts +18 -18
- package/src/route/route.service.spec.ts +300 -300
- package/src/route/route.service.ts +164 -164
- package/src/screen/dto/create.dto.ts +11 -11
- package/src/screen/dto/update.dto.ts +19 -19
- package/src/screen/screen.controller.ts +93 -93
- package/src/screen/screen.module.ts +18 -18
- package/src/screen/screen.service.spec.ts +298 -298
- package/src/screen/screen.service.ts +179 -179
- package/src/types/http-method.ts +8 -8
- package/src/user/constants/user.constants.ts +1 -1
- package/src/user/dto/create.dto.ts +24 -24
- package/src/user/dto/update.dto.ts +41 -41
- package/src/user/user.controller.ts +75 -75
- package/src/user/user.module.ts +18 -18
- package/src/user/user.service.spec.ts +294 -294
- package/src/user/user.service.ts +129 -129
- package/tsconfig.lib.json +9 -9
- package/tsconfig.production.json +20 -20
package/src/auth/auth.service.ts
CHANGED
@@ -1,349 +1,355 @@
|
|
1
|
-
import { MailService } from '@hedhog/mail';
|
2
|
-
import { PrismaService } from '@hedhog/prisma';
|
3
|
-
import {
|
4
|
-
BadRequestException,
|
5
|
-
ConflictException,
|
6
|
-
forwardRef,
|
7
|
-
Inject,
|
8
|
-
Injectable,
|
9
|
-
NotFoundException,
|
10
|
-
} from '@nestjs/common';
|
11
|
-
import { ConfigService } from '@nestjs/config';
|
12
|
-
import { JwtService } from '@nestjs/jwt';
|
13
|
-
import { compare, genSalt, hash } from 'bcrypt';
|
14
|
-
import {
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
import {
|
21
|
-
import {
|
22
|
-
import {
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
private readonly
|
32
|
-
@Inject(forwardRef(() =>
|
33
|
-
private readonly
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
}
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
if (!
|
76
|
-
|
77
|
-
}
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
};
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
const
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
});
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
}
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
});
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
const
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
});
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
|
340
|
-
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
}
|
349
|
-
|
1
|
+
import { MailService } from '@hedhog/mail';
|
2
|
+
import { PrismaService } from '@hedhog/prisma';
|
3
|
+
import {
|
4
|
+
BadRequestException,
|
5
|
+
ConflictException,
|
6
|
+
forwardRef,
|
7
|
+
Inject,
|
8
|
+
Injectable,
|
9
|
+
NotFoundException,
|
10
|
+
} from '@nestjs/common';
|
11
|
+
import { ConfigService } from '@nestjs/config';
|
12
|
+
import { JwtService } from '@nestjs/jwt';
|
13
|
+
import { compare, genSalt, hash } from 'bcrypt';
|
14
|
+
import {
|
15
|
+
getChangeEmailEmail,
|
16
|
+
getChangePasswordEmail,
|
17
|
+
getForgetPasswordEmail,
|
18
|
+
getResetPasswordEmail,
|
19
|
+
} from '../emails';
|
20
|
+
import { ChangeDTO } from './dto/change.dto';
|
21
|
+
import { EmailDTO } from './dto/email.dto';
|
22
|
+
import { ForgetDTO } from './dto/forget.dto';
|
23
|
+
import { LoginDTO } from './dto/login.dto';
|
24
|
+
import { OtpDTO } from './dto/otp.dto';
|
25
|
+
import { ResetDTO } from './dto/reset.dto';
|
26
|
+
import { MultifactorType } from './enums/multifactor-type.enum';
|
27
|
+
|
28
|
+
@Injectable()
|
29
|
+
export class AuthService {
|
30
|
+
constructor(
|
31
|
+
private readonly configService: ConfigService,
|
32
|
+
@Inject(forwardRef(() => PrismaService))
|
33
|
+
private readonly prisma: PrismaService,
|
34
|
+
@Inject(forwardRef(() => JwtService))
|
35
|
+
private readonly jwt: JwtService,
|
36
|
+
@Inject(forwardRef(() => MailService))
|
37
|
+
private readonly mail: MailService,
|
38
|
+
) {}
|
39
|
+
|
40
|
+
async verifyToken(token: string) {
|
41
|
+
return this.jwt.verifyAsync(token, {
|
42
|
+
secret: String(process.env.JWT_SECRET),
|
43
|
+
});
|
44
|
+
}
|
45
|
+
|
46
|
+
generateRandomString(length: number): string {
|
47
|
+
const characters =
|
48
|
+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
49
|
+
let result = '';
|
50
|
+
for (let i = 0; i < length; i++) {
|
51
|
+
const randomIndex = Math.floor(Math.random() * characters.length);
|
52
|
+
result += characters.charAt(randomIndex);
|
53
|
+
}
|
54
|
+
return result;
|
55
|
+
}
|
56
|
+
|
57
|
+
generateRandomNumber(): number {
|
58
|
+
const min = 100000;
|
59
|
+
const max = 999999;
|
60
|
+
return Math.floor(Math.random() * (max - min + 1)) + min;
|
61
|
+
}
|
62
|
+
|
63
|
+
async loginWithEmailAndPassword(email: string, password: string) {
|
64
|
+
const user = await this.prisma.user.findFirst({
|
65
|
+
where: {
|
66
|
+
email,
|
67
|
+
},
|
68
|
+
});
|
69
|
+
|
70
|
+
if (!user) {
|
71
|
+
throw new BadRequestException('Acesso negado');
|
72
|
+
}
|
73
|
+
|
74
|
+
const isPasswordValid = await compare(password, user.password);
|
75
|
+
if (!isPasswordValid) {
|
76
|
+
throw new BadRequestException('Acesso negado');
|
77
|
+
}
|
78
|
+
|
79
|
+
if (!user.multifactor_id) {
|
80
|
+
return this.getToken(user);
|
81
|
+
} else {
|
82
|
+
if (user.multifactor_id === MultifactorType.EMAIL) {
|
83
|
+
const code = this.generateRandomNumber();
|
84
|
+
|
85
|
+
await this.prisma.user.update({
|
86
|
+
where: {
|
87
|
+
id: user.id,
|
88
|
+
},
|
89
|
+
data: {
|
90
|
+
code: String(code),
|
91
|
+
},
|
92
|
+
});
|
93
|
+
|
94
|
+
await this.mail.send({
|
95
|
+
to: user.email,
|
96
|
+
subject: 'Código de Login',
|
97
|
+
body: `Seu código de login é ${code}`,
|
98
|
+
});
|
99
|
+
}
|
100
|
+
|
101
|
+
return {
|
102
|
+
name: user.name,
|
103
|
+
email: user.email,
|
104
|
+
token: this.jwt.sign({
|
105
|
+
id: user.id,
|
106
|
+
mfa: user.multifactor_id,
|
107
|
+
}),
|
108
|
+
mfa: true,
|
109
|
+
};
|
110
|
+
}
|
111
|
+
}
|
112
|
+
|
113
|
+
async getToken(user) {
|
114
|
+
delete user.password;
|
115
|
+
|
116
|
+
const payload = { user };
|
117
|
+
|
118
|
+
return {
|
119
|
+
token: this.jwt.sign(payload),
|
120
|
+
};
|
121
|
+
}
|
122
|
+
|
123
|
+
async forget({ email }: ForgetDTO) {
|
124
|
+
const appUrl =
|
125
|
+
process.env.APP_URL ?? this.configService.get<string>('APP_URL');
|
126
|
+
|
127
|
+
const user = await this.prisma.user.findFirst({
|
128
|
+
where: {
|
129
|
+
email,
|
130
|
+
},
|
131
|
+
select: {
|
132
|
+
id: true,
|
133
|
+
},
|
134
|
+
});
|
135
|
+
|
136
|
+
if (user) {
|
137
|
+
const payload = {
|
138
|
+
...user,
|
139
|
+
};
|
140
|
+
|
141
|
+
const code = this.jwt.sign(payload);
|
142
|
+
|
143
|
+
await this.prisma.user.update({
|
144
|
+
where: {
|
145
|
+
id: user.id,
|
146
|
+
},
|
147
|
+
data: {
|
148
|
+
code,
|
149
|
+
},
|
150
|
+
});
|
151
|
+
|
152
|
+
await this.mail.send({
|
153
|
+
to: email,
|
154
|
+
subject: `Recuperação de Senha`,
|
155
|
+
body: getForgetPasswordEmail(
|
156
|
+
`${appUrl}/login?mode=reset-password&code=${code}`,
|
157
|
+
),
|
158
|
+
});
|
159
|
+
}
|
160
|
+
|
161
|
+
return {
|
162
|
+
message:
|
163
|
+
'Se este e-mail estiver cadastrado, você receberá instruções para redefinir sua senha.',
|
164
|
+
};
|
165
|
+
}
|
166
|
+
|
167
|
+
async changePassword({
|
168
|
+
email,
|
169
|
+
currentPassword,
|
170
|
+
newPassword,
|
171
|
+
confirmNewPassword,
|
172
|
+
}: ChangeDTO) {
|
173
|
+
if (newPassword !== confirmNewPassword) {
|
174
|
+
throw new BadRequestException('Senhas não conferem');
|
175
|
+
}
|
176
|
+
|
177
|
+
const user = await this.prisma.user.findFirst({
|
178
|
+
where: { email },
|
179
|
+
});
|
180
|
+
|
181
|
+
if (!(await compare(currentPassword, user.password))) {
|
182
|
+
throw new NotFoundException('Não foi possível alterar a senha.');
|
183
|
+
}
|
184
|
+
|
185
|
+
const salt = await genSalt();
|
186
|
+
const password = await hash(newPassword, salt);
|
187
|
+
|
188
|
+
const newUser = await this.prisma.user.update({
|
189
|
+
where: {
|
190
|
+
id: user.id,
|
191
|
+
},
|
192
|
+
data: {
|
193
|
+
password,
|
194
|
+
},
|
195
|
+
});
|
196
|
+
|
197
|
+
await this.mail.send({
|
198
|
+
to: email,
|
199
|
+
subject: `Senha alterada`,
|
200
|
+
body: getChangePasswordEmail(),
|
201
|
+
});
|
202
|
+
|
203
|
+
return this.getToken(newUser);
|
204
|
+
}
|
205
|
+
|
206
|
+
async changeEmail({ currentEmail, password, newEmail }: EmailDTO) {
|
207
|
+
const user = await this.prisma.user.findFirst({
|
208
|
+
where: { email: currentEmail },
|
209
|
+
});
|
210
|
+
|
211
|
+
if (!user) {
|
212
|
+
throw new BadRequestException('Não foi possível atualizar o e-mail.');
|
213
|
+
}
|
214
|
+
|
215
|
+
if (!(await compare(password, user.password))) {
|
216
|
+
throw new BadRequestException('Não foi possível atualizar o e-mail.');
|
217
|
+
}
|
218
|
+
|
219
|
+
const existingUser = await this.prisma.user.findFirst({
|
220
|
+
where: { email: newEmail },
|
221
|
+
});
|
222
|
+
|
223
|
+
if (existingUser) {
|
224
|
+
throw new ConflictException('Não foi possível atualizar o e-mail.');
|
225
|
+
}
|
226
|
+
|
227
|
+
const newUser = await this.prisma.user.updateMany({
|
228
|
+
where: { email: currentEmail },
|
229
|
+
data: { email: newEmail },
|
230
|
+
});
|
231
|
+
|
232
|
+
const personUser = await this.prisma.person_user.findFirst({
|
233
|
+
where: { user_id: user.id },
|
234
|
+
select: { person_id: true },
|
235
|
+
});
|
236
|
+
|
237
|
+
if (!personUser) {
|
238
|
+
throw new NotFoundException('Erro ao atualizar os dados do usuário.');
|
239
|
+
}
|
240
|
+
|
241
|
+
const { id: emailContactTypeId } =
|
242
|
+
await this.prisma.person_contact_type.findFirst({
|
243
|
+
where: { slug: 'EMAIL' },
|
244
|
+
});
|
245
|
+
|
246
|
+
await this.prisma.person_contact.updateMany({
|
247
|
+
where: {
|
248
|
+
person_id: personUser.person_id,
|
249
|
+
type_id: emailContactTypeId,
|
250
|
+
},
|
251
|
+
data: { value: newEmail },
|
252
|
+
});
|
253
|
+
|
254
|
+
await this.mail.send({
|
255
|
+
to: newEmail,
|
256
|
+
subject: `Email alterado`,
|
257
|
+
body: getChangeEmailEmail(),
|
258
|
+
});
|
259
|
+
|
260
|
+
return this.getToken(newUser);
|
261
|
+
}
|
262
|
+
|
263
|
+
async resetPassword({ code, newPassword, confirmNewPassword }: ResetDTO) {
|
264
|
+
if (newPassword !== confirmNewPassword) {
|
265
|
+
throw new BadRequestException('Senhas não conferem');
|
266
|
+
}
|
267
|
+
|
268
|
+
try {
|
269
|
+
const decodedCode = this.jwt.decode(code);
|
270
|
+
|
271
|
+
console.log({ decodedCode });
|
272
|
+
|
273
|
+
const { id } = decodedCode;
|
274
|
+
|
275
|
+
const user = await this.prisma.user.findFirst({
|
276
|
+
where: {
|
277
|
+
id,
|
278
|
+
code,
|
279
|
+
},
|
280
|
+
});
|
281
|
+
|
282
|
+
if (user) {
|
283
|
+
const salt = await genSalt();
|
284
|
+
const password = await hash(confirmNewPassword, salt);
|
285
|
+
|
286
|
+
await this.prisma.user.update({
|
287
|
+
where: {
|
288
|
+
id: user.id,
|
289
|
+
},
|
290
|
+
data: {
|
291
|
+
password,
|
292
|
+
code: null,
|
293
|
+
},
|
294
|
+
});
|
295
|
+
|
296
|
+
await this.mail.send({
|
297
|
+
to: user.email,
|
298
|
+
subject: `Senha recuperada`,
|
299
|
+
body: getResetPasswordEmail(),
|
300
|
+
});
|
301
|
+
|
302
|
+
return this.getToken(user);
|
303
|
+
}
|
304
|
+
|
305
|
+
return false;
|
306
|
+
} catch (error: any) {
|
307
|
+
throw new BadRequestException(
|
308
|
+
`Invalid code. ${error?.message ?? String(error)}`,
|
309
|
+
);
|
310
|
+
}
|
311
|
+
}
|
312
|
+
|
313
|
+
async otp({ token, code }: OtpDTO) {
|
314
|
+
const data = this.jwt.decode(token);
|
315
|
+
|
316
|
+
const user = await this.prisma.user.findFirst({
|
317
|
+
where: {
|
318
|
+
id: data['id'],
|
319
|
+
code: String(code),
|
320
|
+
},
|
321
|
+
});
|
322
|
+
|
323
|
+
if (!user) {
|
324
|
+
throw new NotFoundException('Código inválido');
|
325
|
+
}
|
326
|
+
|
327
|
+
await this.prisma.user.update({
|
328
|
+
where: {
|
329
|
+
id: user.id,
|
330
|
+
},
|
331
|
+
data: {
|
332
|
+
code: null,
|
333
|
+
},
|
334
|
+
});
|
335
|
+
|
336
|
+
return this.getToken(user);
|
337
|
+
}
|
338
|
+
|
339
|
+
async login({ email, password }: LoginDTO) {
|
340
|
+
/*
|
341
|
+
await this.mail.send({
|
342
|
+
to: email,
|
343
|
+
subject: `Novo login no CoinBitClub`,
|
344
|
+
body: getUserLoginEmail(),
|
345
|
+
});
|
346
|
+
*/
|
347
|
+
return this.loginWithEmailAndPassword(email, password);
|
348
|
+
}
|
349
|
+
|
350
|
+
async verify(id: number) {
|
351
|
+
return this.prisma.user.findUnique({
|
352
|
+
where: { id },
|
353
|
+
});
|
354
|
+
}
|
355
|
+
}
|