-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsettings-security-password.component.ts
More file actions
200 lines (187 loc) · 6.32 KB
/
Copy pathsettings-security-password.component.ts
File metadata and controls
200 lines (187 loc) · 6.32 KB
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
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
import {
Component,
EventEmitter,
Input,
OnDestroy,
OnInit,
Output,
ViewChild,
} from '@angular/core'
import {
UntypedFormBuilder,
UntypedFormControl,
UntypedFormGroup,
Validators,
} from '@angular/forms'
import { Subject, takeUntil } from 'rxjs'
import { HAS_LETTER_OR_SYMBOL, HAS_NUMBER } from 'src/app/constants'
import { AccountSecurityPasswordService } from 'src/app/core/account-security-password/account-security-password.service'
import { RegisterService } from 'src/app/core/register/register.service'
import { OrcidValidators } from 'src/app/validators'
import { AuthChallengeComponent } from '@orcid/registry-ui'
import { ErrorStateMatcherForTwoFactorFields } from '../../../sign-in/ErrorStateMatcherForTwoFactorFields'
import { MatDialog } from '@angular/material/dialog'
import { AuthChallengeFormData } from '../../../types/common.endpoint'
@Component({
selector: 'app-settings-security-password',
templateUrl: './settings-security-password.component.html',
styleUrls: [
'./settings-security-password.component.scss',
'./settings-security-password.component.scss-theme.scss',
],
standalone: false,
})
export class SettingsSecurityPasswordComponent implements OnInit, OnDestroy {
form: UntypedFormGroup
hasNumberPattern = HAS_NUMBER
hasLetterOrSymbolPattern = HAS_LETTER_OR_SYMBOL
@Input() twoFactorState: boolean
@Output() loading = new EventEmitter<boolean>()
@ViewChild(AuthChallengeComponent)
authChallengeComponent: AuthChallengeComponent
errors: string[]
success: boolean
cancel: boolean
$destroy = new Subject<void>()
currentValidate8orMoreCharactersStatus: boolean
currentValidateAtLeastALetterOrSymbolStatus: boolean
currentValidateAtLeastANumber: boolean
confirmPasswordPlaceholder = $localize`:@@accountSettings.security.password.confirmPasswordPlaceholder:Confirm your new password`
authChallengeLabel = $localize`:@@accountSettings.security.password.authChallengeLabel:to complete your password reset`
errorMatcher = new ErrorStateMatcherForTwoFactorFields()
constructor(
private _fb: UntypedFormBuilder,
private _register: RegisterService,
private _accountPassword: AccountSecurityPasswordService,
private _dialog: MatDialog
) {}
ngOnInit(): void {
this.form = this._fb.group(
{
oldPassword: ['', Validators.required],
password: new UntypedFormControl('', {
validators: [
Validators.required,
Validators.minLength(8),
Validators.maxLength(256),
Validators.pattern(this.hasNumberPattern),
Validators.pattern(this.hasLetterOrSymbolPattern),
],
asyncValidators: [this._register.backendValueValidate('password')],
}),
retypedPassword: new UntypedFormControl('', Validators.required),
twoFactorCode: new UntypedFormControl(null, [
Validators.minLength(6),
Validators.maxLength(6),
]),
twoFactorRecoveryCode: new UntypedFormControl(null, [
Validators.minLength(10),
Validators.maxLength(10),
]),
},
{
validators: OrcidValidators.matchValues('password', 'retypedPassword'),
}
)
}
openAuthChallenge() {
const dialogRef = this._dialog.open<AuthChallengeComponent>(
AuthChallengeComponent,
{
data: {
parentForm: this.form,
showPasswordField: false,
actionDescription: this.authChallengeLabel,
} as AuthChallengeFormData,
}
)
dialogRef.componentInstance.submitAttempt
.pipe(takeUntil(dialogRef.afterClosed()))
.subscribe(() => {
this._accountPassword.updatePassword(this.form.value).subscribe({
next: (response: any) => {
if (response.success) {
dialogRef.close(true)
} else {
dialogRef.componentInstance.loading = false
dialogRef.componentInstance.processBackendResponse(response)
}
},
})
})
dialogRef.afterClosed().subscribe((success) => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur()
}
this.form.reset()
if (success) {
this.success = true
} else {
this.cancel = true
}
})
}
save() {
this.form.markAllAsTouched()
this.success = false
this.cancel = false
if (this.form.valid) {
this.loading.emit(true)
this._accountPassword
.updatePassword(this.form.value)
.subscribe((value) => {
this.loading.emit(false)
if (value.passwordContainsEmail) {
this.form.controls['password']?.setErrors({ containsEmail: true })
}
if (value.success) {
setTimeout(() => {
if (document.activeElement instanceof HTMLElement) {
document.activeElement.blur()
this.form.reset()
}
})
this.success = true
} else {
if (value.errors && value.errors.length > 0) {
this.form.controls['oldPassword'].setErrors({
backendErrors: value.errors || null,
})
} else if (value.twoFactorEnabled && !value.passwordContainsEmail) {
this.openAuthChallenge()
}
}
})
}
}
get validate8orMoreCharacters() {
const status =
this.form.hasError('required', 'password') ||
this.form.hasError('minlength', 'password')
this.currentValidate8orMoreCharactersStatus = status
return status
}
get validateAtLeastALetterOrSymbol() {
const status =
!(this.form.value?.password as string)?.trim().length ||
this.form.hasError('required', 'password') ||
this.form.getError('pattern', 'password')?.requiredPattern ==
this.hasLetterOrSymbolPattern
this.currentValidateAtLeastALetterOrSymbolStatus = status
return status
}
get validateAtLeastANumber() {
const status =
!(this.form.value?.password as string)?.trim().length ||
this.form.hasError('required', 'password') ||
this.form.getError('pattern', 'password')?.requiredPattern ==
this.hasNumberPattern
this.currentValidateAtLeastANumber = status
return status
}
ngOnDestroy(): void {
this.form?.reset()
this.$destroy.next()
this.$destroy.complete()
}
}