diff --git a/libs/auth/src/angular/login/login.component.ts b/libs/auth/src/angular/login/login.component.ts index ad17a0a97a..239383ddd0 100644 --- a/libs/auth/src/angular/login/login.component.ts +++ b/libs/auth/src/angular/login/login.component.ts @@ -18,7 +18,8 @@ import { DevicesApiServiceAbstraction } from "@bitwarden/common/auth/abstraction import { CaptchaIFrame } from "@bitwarden/common/auth/captcha-iframe"; import { AuthResult } from "@bitwarden/common/auth/models/domain/auth-result"; import { ForceSetPasswordReason } from "@bitwarden/common/auth/models/domain/force-set-password-reason"; -import { ClientType } from "@bitwarden/common/enums"; +import { ClientType, HttpStatusCode } from "@bitwarden/common/enums"; +import { ErrorResponse } from "@bitwarden/common/models/response/error.response"; import { AppIdService } from "@bitwarden/common/platform/abstractions/app-id.service"; import { BroadcasterService } from "@bitwarden/common/platform/abstractions/broadcaster.service"; import { EnvironmentService } from "@bitwarden/common/platform/abstractions/environment.service"; @@ -26,6 +27,7 @@ import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.servic import { LogService } from "@bitwarden/common/platform/abstractions/log.service"; import { MessagingService } from "@bitwarden/common/platform/abstractions/messaging.service"; import { PlatformUtilsService } from "@bitwarden/common/platform/abstractions/platform-utils.service"; +import { ValidationService } from "@bitwarden/common/platform/abstractions/validation.service"; import { Utils } from "@bitwarden/common/platform/misc/utils"; import { SyncService } from "@bitwarden/common/platform/sync"; import { PasswordStrengthServiceAbstraction } from "@bitwarden/common/tools/password-strength"; @@ -136,6 +138,7 @@ export class LoginComponent implements OnInit, OnDestroy { private syncService: SyncService, private toastService: ToastService, private logService: LogService, + private validationService: ValidationService, ) { this.clientType = this.platformUtilsService.getClientType(); this.loginViaAuthRequestSupported = this.loginComponentService.isLoginViaAuthRequestSupported(); @@ -182,19 +185,54 @@ export class LoginComponent implements OnInit, OnDestroy { null, ); - const authResult = await this.loginStrategyService.logIn(credentials); + try { + const authResult = await this.loginStrategyService.logIn(credentials); - await this.saveEmailSettings(); - await this.handleAuthResult(authResult); + await this.saveEmailSettings(); + await this.handleAuthResult(authResult); - if (this.clientType === ClientType.Desktop) { - if (this.captchaSiteKey) { - const content = document.getElementById("content") as HTMLDivElement; - content.setAttribute("style", "width:335px"); + if (this.clientType === ClientType.Desktop) { + if (this.captchaSiteKey) { + const content = document.getElementById("content") as HTMLDivElement; + content.setAttribute("style", "width:335px"); + } } + } catch (error) { + this.logService.error(error); + this.handleSubmitError(error); } }; + /** + * Handles the error from the submit function. + * + * @param error The error object. + */ + private handleSubmitError(error: unknown) { + // Handle error responses + if (error instanceof ErrorResponse) { + switch (error.statusCode) { + case HttpStatusCode.BadRequest: { + if (error.message.toLowerCase().includes("username or password is incorrect")) { + this.formGroup.controls.masterPassword.setErrors({ + error: { + message: this.i18nService.t("invalidMasterPassword"), + }, + }); + } + break; + } + default: { + // Allow all other errors to be handled by toast + this.validationService.showError(error); + } + } + } else { + // Allow all other errors to be handled by toast + this.validationService.showError(error); + } + } + /** * Handles the result of the authentication process. *