-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathtwo-factor-enable.component.ts
More file actions
132 lines (119 loc) · 3.72 KB
/
Copy pathtwo-factor-enable.component.ts
File metadata and controls
132 lines (119 loc) · 3.72 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
import { Component, EventEmitter, OnInit, Output } from '@angular/core'
import { first } from 'rxjs/operators'
import {
UntypedFormControl,
UntypedFormGroup,
Validators,
} from '@angular/forms'
import {
MAT_TOOLTIP_DEFAULT_OPTIONS,
MatTooltipDefaultOptions,
} from '@angular/material/tooltip'
import { TwoFactorAuthenticationService } from '../../../core/two-factor-authentication/two-factor-authentication.service'
import { TwoFactor } from '../../../types/two-factor.endpoint'
import { RumJourneyEventService } from '../../../rum/service/customEvent.service'
import { AppEventName } from '../../../rum/app-event-names'
declare const runtimeEnvironment: any
declare const $localize: any
export const clipboardTooltip: MatTooltipDefaultOptions = {
showDelay: 500,
hideDelay: 500,
touchendHideDelay: 500,
}
@Component({
selector: 'app-two-factor-enable',
templateUrl: './two-factor-enable.component.html',
styleUrls: [
'./two-factor-enable.component.scss',
'./two-factor-enable.component.scss-theme.scss',
],
providers: [
{ provide: MAT_TOOLTIP_DEFAULT_OPTIONS, useValue: clipboardTooltip },
],
preserveWhitespaces: true,
standalone: false,
})
export class TwoFactorEnableComponent implements OnInit {
@Output() twoFactorEnabled = new EventEmitter<{
backupCodes?: string
backupCodesClipboard?: string
}>()
environment = runtimeEnvironment
twoFactorForm: UntypedFormGroup
showTextCode = false
loading = false
textCodeTooltip = $localize`:@@account.copySetupCodeToClipboard:Copy setup code to clipboard`
constructor(
private _twoFactorService: TwoFactorAuthenticationService,
private _observability: RumJourneyEventService
) {}
ngOnInit(): void {
this._observability.recordSimpleEvent(
AppEventName.TwoFactorSetupStep1Loaded
)
this.twoFactorForm = new UntypedFormGroup({
verificationCode: new UntypedFormControl('', [
Validators.required,
Validators.minLength(6),
Validators.maxLength(6),
]),
})
}
onSubmit() {
const verificationCodeControl = this.twoFactorForm.get('verificationCode')
if (verificationCodeControl?.hasError('invalidCode')) {
const errors = { ...(verificationCodeControl.errors || {}) }
delete errors.invalidCode
verificationCodeControl.setErrors(
Object.keys(errors).length ? errors : null
)
}
if (this.twoFactorForm.invalid) {
this.twoFactorForm.markAllAsTouched()
return
}
this.loading = true
const twoFactor: TwoFactor = {
verificationCode: this.twoFactorForm.get('verificationCode')?.value,
}
this._twoFactorService.register(twoFactor).subscribe({
next: (res) => {
this.loading = false
if (!res.valid) {
verificationCodeControl?.setErrors({
...(verificationCodeControl.errors || {}),
invalidCode: true,
})
} else if (res.valid) {
const backupCodes = (res.backupCodes || []).join('\n')
this.twoFactorEnabled.emit({
backupCodes,
backupCodesClipboard: (res.backupCodes || []).join(' '),
})
}
},
error: () => {
this.loading = false
verificationCodeControl?.setErrors({
...(verificationCodeControl.errors || {}),
invalidCode: true,
})
},
})
}
textCode() {
this._twoFactorService
.getTextCode()
.pipe(first())
.subscribe((result) => {
this.showTextCode = true
this.twoFactorForm.addControl(
'textCode',
new UntypedFormControl({ value: result.secret, disabled: true }, {})
)
})
}
get textCodeClipboard() {
return this.twoFactorForm.get('textCode')?.value || ''
}
}