PM-13114 - WebEnvSvc - setEnv now navigates to region (#11365)

This commit is contained in:
Jared Snider 2024-10-02 14:21:22 -04:00 committed by GitHub
parent c969f617a8
commit 398f9be351
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 4 deletions

View File

@ -1,5 +1,6 @@
import { CommonModule } from "@angular/common";
import { APP_INITIALIZER, NgModule, Optional, SkipSelf } from "@angular/core";
import { Router } from "@angular/router";
import {
CollectionAdminService,
@ -175,7 +176,7 @@ const safeProviders: SafeProvider[] = [
safeProvider({
provide: EnvironmentService,
useClass: WebEnvironmentService,
deps: [WINDOW, StateProvider, AccountService],
deps: [WINDOW, StateProvider, AccountService, Router],
}),
safeProvider({
provide: BiometricsService,

View File

@ -1,3 +1,4 @@
import { Router } from "@angular/router";
import { ReplaySubject } from "rxjs";
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
@ -23,6 +24,7 @@ export class WebEnvironmentService extends DefaultEnvironmentService {
private win: Window,
stateProvider: StateProvider,
accountService: AccountService,
private router: Router,
) {
super(stateProvider, accountService);
@ -47,9 +49,34 @@ export class WebEnvironmentService extends DefaultEnvironmentService {
this.environment$ = subject.asObservable();
}
// Web cannot set environment
async setEnvironment(region: Region, urls?: Urls): Promise<Urls> {
return;
// Web setting env means navigating to a new location
setEnvironment(region: Region, urls?: Urls): Promise<Urls> {
if (region === Region.SelfHosted) {
throw new Error("setEnvironment does not work in web for self-hosted.");
}
const currentDomain = Utils.getDomain(this.win.location.href);
const currentRegion = this.availableRegions().find(
(r) => Utils.getDomain(r.urls.webVault) === currentDomain,
);
if (currentRegion.key === region) {
// They have selected the current region, nothing to do
return Promise.resolve(currentRegion.urls);
}
const chosenRegion = this.availableRegions().find((r) => r.key === region);
if (chosenRegion == null) {
throw new Error("The selected region is not known as an available region.");
}
// Preserve the current in app route + params in the new location
const routeAndParams = `/#${this.router.url}`;
this.win.location.href = chosenRegion.urls.webVault + routeAndParams;
// This return shouldn't matter as we are about to leave the current window
return Promise.resolve(chosenRegion.urls);
}
}