1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-18 01:03:35 +00:00

[authService refactor] Fix browser by not using instanceof (#647)

* use authenticationType enum instead of instanceof
This commit is contained in:
Thomas Rittson
2022-02-07 07:33:19 +10:00
committed by GitHub
parent 380a7c7ee5
commit 9caea70ea2
3 changed files with 20 additions and 7 deletions

View File

@@ -28,6 +28,8 @@ import { StateService } from "../abstractions/state.service";
import { TokenService } from "../abstractions/token.service";
import { TwoFactorService } from "../abstractions/twoFactor.service";
import { AuthenticationType } from "../enums/authenticationType";
export class AuthService implements AuthServiceAbstraction {
get email(): string {
return this.logInStrategy instanceof PasswordLogInStrategy ? this.logInStrategy.email : null;
@@ -60,10 +62,9 @@ export class AuthService implements AuthServiceAbstraction {
): Promise<AuthResult> {
this.clearState();
let result: AuthResult;
let strategy: ApiLogInStrategy | PasswordLogInStrategy | SsoLogInStrategy;
if (credentials instanceof PasswordLogInCredentials) {
if (credentials.type === AuthenticationType.Password) {
strategy = new PasswordLogInStrategy(
this.cryptoService,
this.apiService,
@@ -76,8 +77,7 @@ export class AuthService implements AuthServiceAbstraction {
this.twoFactorService,
this
);
result = await strategy.logIn(credentials);
} else if (credentials instanceof SsoLogInCredentials) {
} else if (credentials.type === AuthenticationType.Sso) {
strategy = new SsoLogInStrategy(
this.cryptoService,
this.apiService,
@@ -90,8 +90,7 @@ export class AuthService implements AuthServiceAbstraction {
this.twoFactorService,
this.keyConnectorService
);
result = await strategy.logIn(credentials);
} else if (credentials instanceof ApiLogInCredentials) {
} else if (credentials.type === AuthenticationType.Api) {
strategy = new ApiLogInStrategy(
this.cryptoService,
this.apiService,
@@ -105,9 +104,10 @@ export class AuthService implements AuthServiceAbstraction {
this.environmentService,
this.keyConnectorService
);
result = await strategy.logIn(credentials);
}
const result = await strategy.logIn(credentials as any);
if (result?.requiresTwoFactor) {
this.saveState(strategy);
}