mirror of
https://github.com/bitwarden/browser
synced 2025-12-12 06:13:38 +00:00
PM-13114 - WebEnvSvc - setEnv now navigates to region (#11365)
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { CommonModule } from "@angular/common";
|
import { CommonModule } from "@angular/common";
|
||||||
import { APP_INITIALIZER, NgModule, Optional, SkipSelf } from "@angular/core";
|
import { APP_INITIALIZER, NgModule, Optional, SkipSelf } from "@angular/core";
|
||||||
|
import { Router } from "@angular/router";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
CollectionAdminService,
|
CollectionAdminService,
|
||||||
@@ -175,7 +176,7 @@ const safeProviders: SafeProvider[] = [
|
|||||||
safeProvider({
|
safeProvider({
|
||||||
provide: EnvironmentService,
|
provide: EnvironmentService,
|
||||||
useClass: WebEnvironmentService,
|
useClass: WebEnvironmentService,
|
||||||
deps: [WINDOW, StateProvider, AccountService],
|
deps: [WINDOW, StateProvider, AccountService, Router],
|
||||||
}),
|
}),
|
||||||
safeProvider({
|
safeProvider({
|
||||||
provide: BiometricsService,
|
provide: BiometricsService,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { Router } from "@angular/router";
|
||||||
import { ReplaySubject } from "rxjs";
|
import { ReplaySubject } from "rxjs";
|
||||||
|
|
||||||
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
import { AccountService } from "@bitwarden/common/auth/abstractions/account.service";
|
||||||
@@ -23,6 +24,7 @@ export class WebEnvironmentService extends DefaultEnvironmentService {
|
|||||||
private win: Window,
|
private win: Window,
|
||||||
stateProvider: StateProvider,
|
stateProvider: StateProvider,
|
||||||
accountService: AccountService,
|
accountService: AccountService,
|
||||||
|
private router: Router,
|
||||||
) {
|
) {
|
||||||
super(stateProvider, accountService);
|
super(stateProvider, accountService);
|
||||||
|
|
||||||
@@ -47,9 +49,34 @@ export class WebEnvironmentService extends DefaultEnvironmentService {
|
|||||||
this.environment$ = subject.asObservable();
|
this.environment$ = subject.asObservable();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Web cannot set environment
|
// Web setting env means navigating to a new location
|
||||||
async setEnvironment(region: Region, urls?: Urls): Promise<Urls> {
|
setEnvironment(region: Region, urls?: Urls): Promise<Urls> {
|
||||||
return;
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user