1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-13 06:43:35 +00:00

[pm-5287] fix account switch logout routing (#7231)

* Navigate to home from account switcher

Also updates the main background handling of logout to either finish switch or logout, depending on which occurred

* Prefer observable guards

we were racing the account switch process on `accountService` and this async guard. It only depended on account status, which is available from `accountService`, so the correct move was to observe that status.

The unauthGuardFn allows for updating homepage depending on window state because popout windows have different nav to other locations.
This commit is contained in:
Matt Gibson
2023-12-18 14:23:43 -05:00
committed by GitHub
parent b4999866fa
commit 87b6651f8e
4 changed files with 60 additions and 16 deletions

View File

@@ -1,9 +1,14 @@
import { Injectable } from "@angular/core";
import { CanActivate, Router } from "@angular/router";
import { Injectable, inject } from "@angular/core";
import { CanActivate, CanActivateFn, Router, UrlTree } from "@angular/router";
import { Observable, map } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
import { AuthService } from "@bitwarden/common/auth/abstractions/auth.service";
import { AuthenticationStatus } from "@bitwarden/common/auth/enums/authentication-status";
/**
* @deprecated use unauthGuardFn function instead
*/
@Injectable()
export class UnauthGuard implements CanActivate {
protected homepage = "vault";
@@ -26,3 +31,34 @@ export class UnauthGuard implements CanActivate {
return this.router.createUrlTree([this.homepage]);
}
}
type UnauthRoutes = {
homepage: () => string;
locked: string;
};
const defaultRoutes: UnauthRoutes = {
homepage: () => "/vault",
locked: "/lock",
};
function unauthGuard(routes: UnauthRoutes): Observable<boolean | UrlTree> {
const accountService = inject(AccountService);
const router = inject(Router);
return accountService.activeAccount$.pipe(
map((accountData) => {
if (accountData == null || accountData.status === AuthenticationStatus.LoggedOut) {
return true;
} else if (accountData.status === AuthenticationStatus.Locked) {
return router.createUrlTree([routes.locked]);
} else {
return router.createUrlTree([routes.homepage()]);
}
}),
);
}
export function unauthGuardFn(overrides: Partial<UnauthRoutes> = {}): CanActivateFn {
return () => unauthGuard({ ...defaultRoutes, ...overrides });
}