From a5cf61b597a206a63432982c4eb4b92f704a5b21 Mon Sep 17 00:00:00 2001 From: Jared Snider Date: Mon, 9 Dec 2024 19:20:21 -0500 Subject: [PATCH] PM-8113 - TwoFactorAuthComp - More refactoring --- .../two-factor-auth.component.ts | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts b/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts index afc006d5a0..86b6499dcb 100644 --- a/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts +++ b/libs/auth/src/angular/two-factor-auth/two-factor-auth.component.ts @@ -5,7 +5,7 @@ import { Component, Inject, OnDestroy, OnInit, ViewChild } from "@angular/core"; import { takeUntilDestroyed } from "@angular/core/rxjs-interop"; import { FormBuilder, ReactiveFormsModule, Validators } from "@angular/forms"; import { ActivatedRoute, NavigationExtras, Router, RouterLink } from "@angular/router"; -import { Subject, takeUntil, lastValueFrom, firstValueFrom } from "rxjs"; +import { lastValueFrom, firstValueFrom } from "rxjs"; import { JslibModule } from "@bitwarden/angular/jslib.module"; import { I18nPipe } from "@bitwarden/angular/platform/pipes/i18n.pipe"; @@ -88,7 +88,8 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { providerData: any; @ViewChild("duoComponent") duoComponent!: TwoFactorAuthDuoComponent; - formGroup = this.formBuilder.group({ + + form = this.formBuilder.group({ token: [ "", { @@ -100,9 +101,8 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { }); actionButtonText = ""; title = ""; - formPromise: Promise; - private destroy$ = new Subject(); + formPromise: Promise; onSuccessfulLoginTde: () => Promise; onSuccessfulLoginTdeNavigate: () => Promise; @@ -142,9 +142,9 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { async ngOnInit() { // TODO: should this be in a guard instead of here? - const authenticating = await this.authenticating(); + const userIsAuthenticating = await this.userIsAuthenticating(); const twoFactorProviders = await this.twoFactorService.getProviders(); - if (!authenticating || twoFactorProviders == null) { + if (!userIsAuthenticating || twoFactorProviders == null) { await this.router.navigate([this.loginRoute]); return; } @@ -157,10 +157,11 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { return providers.get(this.selectedProviderType); }); this.providerData = providerData; - await this.updateUIToProviderData(); + await this.setTitleByTwoFactorProvider(); this.actionButtonText = this.i18nService.t("continue"); - this.formGroup.valueChanges.pipe(takeUntil(this.destroy$)).subscribe((value) => { + + this.form.valueChanges.pipe(takeUntilDestroyed()).subscribe((value) => { this.token = value.token; this.remember = value.remember; }); @@ -254,7 +255,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { }); this.providerData = providerData; this.selectedProviderType = response.type; - await this.updateUIToProviderData(); + await this.setTitleByTwoFactorProvider(); } } @@ -287,7 +288,7 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { return true; } - async updateUIToProviderData() { + async setTitleByTwoFactorProvider() { if (this.selectedProviderType == null) { this.title = this.i18nService.t("loginUnavailable"); return; @@ -455,15 +456,10 @@ export class TwoFactorAuthComponent implements OnInit, OnDestroy { } } - private async authenticating(): Promise { + private async userIsAuthenticating(): Promise { return (await firstValueFrom(this.loginStrategyService.currentAuthType$)) !== null; } - private async needsLock(): Promise { - const authType = await firstValueFrom(this.loginStrategyService.currentAuthType$); - return authType == AuthenticationType.Sso || authType == AuthenticationType.UserApiKey; - } - async isLinux() { // TODO: this was extension logic and must be moved to service if platform utils service doesn't have support for this // return (await BrowserApi.getPlatformInfo()).os === "linux";