UNPKG

4.08 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const common_1 = require("@nestjs/common");
5const uuidv4 = require("uuid/v4");
6const transactional_1 = require("../datastore/transactional");
7const logging_1 = require("../gcloud/logging");
8const index_1 = require("../index");
9const auth_repository_1 = require("./auth.repository");
10const auth_service_1 = require("./auth.service");
11const DEFAULT_PASSWORD_TOKEN_EXPIRY = 24 * 60 * 60 * 1000;
12let PasswordResetService = class PasswordResetService {
13 constructor(authRepository, passwordResetRepository, configuration, mailSender) {
14 this.authRepository = authRepository;
15 this.passwordResetRepository = passwordResetRepository;
16 this.configuration = configuration;
17 this.mailSender = mailSender;
18 this.logger = logging_1.createLogger('password-reset-service');
19 this.tokenExpiry = configuration.passwordTokenExpiry || DEFAULT_PASSWORD_TOKEN_EXPIRY;
20 }
21 async resetPassword(context, email) {
22 const credentials = await this.authRepository.get(context, email);
23 if (!credentials) {
24 this.logger.info(`No account found when trying to reset password for "${email}"`);
25 return;
26 }
27 if (credentials.type !== 'password') {
28 this.logger.info(`No account found when trying to reset password for "${email}"`);
29 return;
30 }
31 this.logger.info(`Sending password reset email for "${email}"`);
32 const id = uuidv4();
33 await this.passwordResetRepository.save(context, {
34 accountId: credentials.id,
35 createdAt: new Date(),
36 id,
37 });
38 const address = `${this.configuration.host}/confirm-reset/${id}`;
39 await this.mailSender.send(context, {
40 to: email,
41 subject: 'Password reset',
42 html: `
43 <html>
44 <head></head>
45 <body><a href="${address}">Reset your password</a></body>
46 </html>
47 `,
48 });
49 }
50 async confirmResetPassword(context, code, newPassword) {
51 const resetToken = await this.passwordResetRepository.get(context, code);
52 if (!resetToken) {
53 throw new Error('Invalid password reset token');
54 }
55 if (Date.now() - resetToken.createdAt.getTime() > this.tokenExpiry) {
56 throw new Error('Token has expired');
57 }
58 const account = await this.authRepository.get(context, resetToken.accountId);
59 if (!account) {
60 throw new Error('Account no longer exists');
61 }
62 if (account.type !== 'password') {
63 throw new Error('Account no longer exists');
64 }
65 account.password = await auth_service_1.hashPassword(newPassword);
66 this.logger.info(`Resetting password for account ${resetToken.id}`);
67 await this.passwordResetRepository.delete(context, resetToken.id);
68 await this.authRepository.save(context, account);
69 }
70};
71tslib_1.__decorate([
72 transactional_1.Transactional(),
73 tslib_1.__metadata("design:type", Function),
74 tslib_1.__metadata("design:paramtypes", [Object, String]),
75 tslib_1.__metadata("design:returntype", Promise)
76], PasswordResetService.prototype, "resetPassword", null);
77tslib_1.__decorate([
78 transactional_1.Transactional(),
79 tslib_1.__metadata("design:type", Function),
80 tslib_1.__metadata("design:paramtypes", [Object, String, String]),
81 tslib_1.__metadata("design:returntype", Promise)
82], PasswordResetService.prototype, "confirmResetPassword", null);
83PasswordResetService = tslib_1.__decorate([
84 common_1.Injectable(),
85 tslib_1.__param(2, common_1.Inject('Configuration')),
86 tslib_1.__param(3, common_1.Inject(index_1.MAIL_SENDER)),
87 tslib_1.__metadata("design:paramtypes", [auth_repository_1.CredentialRepository,
88 auth_repository_1.PasswordResetRepository, Object, Object])
89], PasswordResetService);
90exports.PasswordResetService = PasswordResetService;
91//# sourceMappingURL=password-reset.service.js.map
\No newline at end of file