1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-16 08:13:42 +00:00

PM-4951 Migrate Recover Two Factor Component (#9170)

* PM-4951 Migrate Recover Two Factor Component

* PM-4951 Addressed review comments

* PM-4951 Addressed review comments

* update route

* add type safety to data properties

---------

Co-authored-by: rr-bw <102181210+rr-bw@users.noreply.github.com>
This commit is contained in:
KiruthigaManivannan
2024-06-05 23:22:04 +05:30
committed by GitHub
parent 1cec69e377
commit 6e55733873
3 changed files with 97 additions and 109 deletions

View File

@@ -1,4 +1,5 @@
import { Component } from "@angular/core";
import { FormControl, FormGroup, Validators } from "@angular/forms";
import { Router } from "@angular/router";
import { LoginStrategyServiceAbstraction } from "@bitwarden/auth/common";
@@ -6,7 +7,6 @@ import { ApiService } from "@bitwarden/common/abstractions/api.service";
import { TwoFactorRecoveryRequest } from "@bitwarden/common/auth/models/request/two-factor-recovery.request";
import { CryptoService } from "@bitwarden/common/platform/abstractions/crypto.service";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { LogService } from "@bitwarden/common/platform/abstractions/log.service";
import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service";
@Component({
@@ -14,10 +14,11 @@ import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/pl
templateUrl: "recover-two-factor.component.html",
})
export class RecoverTwoFactorComponent {
email: string;
masterPassword: string;
recoveryCode: string;
formPromise: Promise<any>;
protected formGroup = new FormGroup({
email: new FormControl(null, [Validators.required]),
masterPassword: new FormControl(null, [Validators.required]),
recoveryCode: new FormControl(null, [Validators.required]),
});
constructor(
private router: Router,
@@ -26,31 +27,32 @@ export class RecoverTwoFactorComponent {
private i18nService: I18nService,
private cryptoService: CryptoService,
private loginStrategyService: LoginStrategyServiceAbstraction,
private logService: LogService,
) {}
async submit() {
try {
const request = new TwoFactorRecoveryRequest();
request.recoveryCode = this.recoveryCode.replace(/\s/g, "").toLowerCase();
request.email = this.email.trim().toLowerCase();
const key = await this.loginStrategyService.makePreloginKey(
this.masterPassword,
request.email,
);
request.masterPasswordHash = await this.cryptoService.hashMasterKey(this.masterPassword, key);
this.formPromise = this.apiService.postTwoFactorRecover(request);
await this.formPromise;
this.platformUtilsService.showToast(
"success",
null,
this.i18nService.t("twoStepRecoverDisabled"),
);
// FIXME: Verify that this floating promise is intentional. If it is, add an explanatory comment and ensure there is proper error handling.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.router.navigate(["/"]);
} catch (e) {
this.logService.error(e);
}
get email(): string {
return this.formGroup.value.email;
}
get masterPassword(): string {
return this.formGroup.value.masterPassword;
}
get recoveryCode(): string {
return this.formGroup.value.recoveryCode;
}
submit = async () => {
const request = new TwoFactorRecoveryRequest();
request.recoveryCode = this.recoveryCode.replace(/\s/g, "").toLowerCase();
request.email = this.email.trim().toLowerCase();
const key = await this.loginStrategyService.makePreloginKey(this.masterPassword, request.email);
request.masterPasswordHash = await this.cryptoService.hashMasterKey(this.masterPassword, key);
await this.apiService.postTwoFactorRecover(request);
this.platformUtilsService.showToast(
"success",
null,
this.i18nService.t("twoStepRecoverDisabled"),
);
await this.router.navigate(["/"]);
};
}