1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-23 11:43:46 +00:00

PM-2055 Update Two Factor Authenticator Dialog (#8972)

* PM-2055 Update Two Factor Authenticator Dialog

* PM-2055 Added close to disable two factor

* PM-2055 Added a event emitter to capture enabled status
This commit is contained in:
KiruthigaManivannan
2024-06-06 19:23:29 +05:30
committed by GitHub
parent a1442194ae
commit 6fadee7cb4
4 changed files with 119 additions and 125 deletions

View File

@@ -1,4 +1,6 @@
import { Component, OnDestroy, OnInit } from "@angular/core";
import { DIALOG_DATA, DialogConfig, DialogRef } from "@angular/cdk/dialog";
import { Component, EventEmitter, Inject, OnDestroy, OnInit, Output } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";
import { firstValueFrom, map } from "rxjs";
import { ApiService } from "@bitwarden/common/abstractions/api.service";
@@ -38,15 +40,21 @@ export class TwoFactorAuthenticatorComponent
extends TwoFactorBaseComponent
implements OnInit, OnDestroy
{
@Output() onChangeStatus = new EventEmitter<boolean>();
type = TwoFactorProviderType.Authenticator;
key: string;
token: string;
formPromise: Promise<TwoFactorAuthenticatorResponse>;
override componentName = "app-two-factor-authenticator";
private qrScript: HTMLScriptElement;
protected formGroup = new FormGroup({
token: new FormControl(null, [Validators.required]),
});
constructor(
@Inject(DIALOG_DATA) protected data: AuthResponse<TwoFactorAuthenticatorResponse>,
private dialogRef: DialogRef,
apiService: ApiService,
i18nService: I18nService,
userVerificationService: UserVerificationService,
@@ -68,8 +76,9 @@ export class TwoFactorAuthenticatorComponent
this.qrScript.async = true;
}
ngOnInit() {
async ngOnInit() {
window.document.body.appendChild(this.qrScript);
await this.auth(this.data);
}
ngOnDestroy() {
@@ -81,17 +90,24 @@ export class TwoFactorAuthenticatorComponent
return this.processResponse(authResponse.response);
}
submit() {
submit = async () => {
if (this.enabled) {
return super.disable(this.formPromise);
await this.disableAuthentication(this.formPromise);
this.onChangeStatus.emit(this.enabled);
this.close();
} else {
return this.enable();
await this.enable();
this.onChangeStatus.emit(this.enabled);
}
};
private async disableAuthentication(promise: Promise<unknown>) {
return super.disable(promise);
}
protected async enable() {
const request = await this.buildRequestModel(UpdateTwoFactorAuthenticatorRequest);
request.token = this.token;
request.token = this.formGroup.value.token;
request.key = this.key;
return super.enable(async () => {
@@ -102,7 +118,7 @@ export class TwoFactorAuthenticatorComponent
}
private async processResponse(response: TwoFactorAuthenticatorResponse) {
this.token = null;
this.formGroup.get("token").setValue(null);
this.enabled = response.enabled;
this.key = response.key;
const email = await firstValueFrom(
@@ -121,4 +137,15 @@ export class TwoFactorAuthenticatorComponent
});
}, 100);
}
close = () => {
this.dialogRef.close(this.enabled);
};
static open(
dialogService: DialogService,
config: DialogConfig<AuthResponse<TwoFactorAuthenticatorResponse>>,
) {
return dialogService.open<boolean>(TwoFactorAuthenticatorComponent, config);
}
}