-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathsettings-security-two-factor-auth.component.ts
More file actions
103 lines (95 loc) · 3.17 KB
/
Copy pathsettings-security-two-factor-auth.component.ts
File metadata and controls
103 lines (95 loc) · 3.17 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
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core'
import { ApplicationRoutes } from '../../../constants'
import { Router } from '@angular/router'
import { first, takeUntil } from 'rxjs/operators'
import { TwoFactorAuthenticationService } from '../../../core/two-factor-authentication/two-factor-authentication.service'
import { MatDialog } from '@angular/material/dialog'
import { AuthChallengeComponent } from '@orcid/registry-ui'
import { AuthChallengeFormData } from '../../../types/common.endpoint'
import {
UntypedFormBuilder,
UntypedFormGroup,
Validators,
} from '@angular/forms'
import { Status } from '../../../types/two-factor.endpoint'
@Component({
selector: 'app-settings-security-two-factor-auth',
templateUrl: './settings-security-two-factor-auth.component.html',
styleUrls: [
'./settings-security-two-factor-auth.component.scss',
'./settings-security-two-factor-auth.component.scss-theme.scss',
],
standalone: false,
})
export class SettingsSecurityTwoFactorAuthComponent implements OnInit {
@Input() twoFactorInfo: Status | undefined
@Output() twoFactorStateOutput = new EventEmitter<any>()
form: UntypedFormGroup
success = false
cancel = false
authChallengeLabel = $localize`:@@accountSettings.security.disable2FA:to disable 2FA`
constructor(
private _router: Router,
private twoFactorAuthenticationService: TwoFactorAuthenticationService,
private _fb: UntypedFormBuilder,
private _dialog: MatDialog
) {}
ngOnInit(): void {
this.form = this._fb.group({
password: [null, Validators.required],
twoFactorCode: [null, [Validators.minLength(6), Validators.maxLength(6)]],
twoFactorRecoveryCode: [
null,
[Validators.minLength(10), Validators.maxLength(10)],
],
})
}
openAuthChallenge() {
const dialogRef = this._dialog.open<AuthChallengeComponent>(
AuthChallengeComponent,
{
data: {
parentForm: this.form,
actionDescription: this.authChallengeLabel,
} as AuthChallengeFormData,
}
)
dialogRef.componentInstance.submitAttempt
.pipe(takeUntil(dialogRef.afterClosed()))
.subscribe(() => {
this.twoFactorAuthenticationService
.disable(this.form.value)
.pipe(first())
.subscribe({
next: (response: any) => {
if (response.success) {
if (this.twoFactorInfo) {
this.twoFactorInfo.enabled = response.enabled
}
this.twoFactorStateOutput.emit(false)
dialogRef.close(true)
} else {
dialogRef.componentInstance.loading = false
dialogRef.componentInstance.processBackendResponse(response)
}
},
})
})
dialogRef.afterClosed().subscribe((success) => {
if (success) {
this.success = true
} else {
this.cancel = true
}
})
}
twoFactor() {
if (!this.twoFactorInfo?.enabled) {
this._router.navigate([ApplicationRoutes.twoFactorSetup])
} else {
this.cancel = false
this.success = false
this.openAuthChallenge()
}
}
}