-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreset-password.component.ts
143 lines (135 loc) · 4.86 KB
/
reset-password.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
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
138
139
140
141
142
143
import type { OnDestroy, OnInit } from '@angular/core';
import { Component, ChangeDetectionStrategy } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { marker as _ } from '@biesbjerg/ngx-translate-extract-marker';
import { AuthService } from '@core/cache-client/api/auth/auth.service';
import { UsersService } from '@core/cache-client/api/users/users.service';
import { MessageService } from '@core/utility/messages/message.service';
import { Form } from '@shared/inputs/form';
import { makeCustom } from '@shared/inputs/input/input';
import { CustomValidators } from '@shared/inputs/shared/validation/custom-validators';
import { StringInput } from '@shared/inputs/string-input/string-input';
import { Destroyed } from '@shared/utility/classes/destroyed';
import { userValidators } from '@shared/utility/user-validators';
@Component({
selector: 'app-reset-password',
templateUrl: './reset-password.component.html',
styleUrls: ['./reset-password.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class PasswordResetComponent
extends Destroyed
implements OnInit, OnDestroy
{
public passwordResetting?: Promise<unknown>;
public requestingPasswordResetEmail?: Promise<unknown>;
public readonly emailInput = makeCustom(
new StringInput('email-input', null, {
validators: userValidators.email,
kind: 'email',
autocomplete: 'email',
}),
{
leftAddons: [
{
translateKey: _('auth.reset-password.email.email'),
icon: 'authentication-email',
},
],
}
);
public readonly passwordForm = new Form([
makeCustom(
new StringInput('new-password', null, {
validators: userValidators.password,
kind: 'password',
autocomplete: 'new-password',
}),
{
leftAddons: [
{
translateKey: _('auth.reset-password.new-password'),
},
],
}
),
makeCustom(
new StringInput('confirm-password', null, {
kind: 'password',
autocomplete: 'new-password',
}),
{
leftAddons: [
{
translateKey: _('auth.reset-password.confirm'),
},
],
}
),
] as const);
public token?: string;
constructor(
private readonly authService: AuthService,
private readonly activatedRoute: ActivatedRoute,
private readonly messageService: MessageService,
private readonly router: Router,
private readonly usersService: UsersService
) {
super();
}
ngOnInit() {
const params = this.activatedRoute.snapshot.queryParams;
if (params.token) {
if (!/^.+\..+\..+$/u.test(params.token)) {
this.messageService.postMessage({
color: 'danger',
title: _('messages.auth.reset-password.invalid.title'),
body: _('messages.auth.reset-password.invalid.body'),
});
this.token = undefined;
} else {
this.token = params.token;
}
}
this.passwordForm.controls[1].setValidators([
CustomValidators.required(),
userValidators.confirmPassword(
() => this.passwordForm.controls[0].value
),
]);
}
public requestResetPasswordEmail() {
this.requestingPasswordResetEmail = this.usersService
.requestResetPassword(this.emailInput.value!)
.then(() => {
this.messageService.postMessage({
color: 'success',
title: _(
'messages.auth.reset-password.email-success.title'
),
body: _('messages.auth.reset-password.email-success.body'),
});
this.router.navigateByUrl('/');
});
}
public resetPassword() {
if (!this.token) {
errors.error({ message: 'Token is null' });
return;
}
this.passwordResetting = this.authService
.resetPassword(this.token, this.passwordForm.controls[0].value!)
.then(() => {
this.messageService.postMessage({
color: 'success',
title: _('messages.auth.reset-password.password-success'),
});
this.router.navigateByUrl('/login');
});
}
ngOnDestroy() {
this.emailInput.destroy();
this.passwordForm.destroy();
this.destroyed.next(undefined);
}
}