1
0
mirror of https://github.com/bitwarden/browser synced 2025-12-14 15:23:33 +00:00
Files
browser/apps/web/src/app/core/router.service.ts
2024-09-30 13:51:31 -04:00

120 lines
3.5 KiB
TypeScript

import { Injectable } from "@angular/core";
import { Title } from "@angular/platform-browser";
import { ActivatedRoute, NavigationEnd, Router } from "@angular/router";
import { filter, firstValueFrom } from "rxjs";
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
import { Utils } from "@bitwarden/common/platform/misc/utils";
import {
KeyDefinition,
ROUTER_DISK,
StateProvider,
GlobalState,
} from "@bitwarden/common/platform/state";
/**
* Data properties acceptable for use in route objects (see usage in oss-routing.module.ts for example)
*/
export interface RouteDataProperties {
/**
* Title of the current HTML document (shows in browser tab)
*/
titleId?: string;
/**
* doNotSaveUrl - choose to not keep track of the previous URL in memory in the RouterService
*/
doNotSaveUrl?: boolean;
}
const DEEP_LINK_REDIRECT_URL = new KeyDefinition(ROUTER_DISK, "deepLinkRedirectUrl", {
deserializer: (value: string) => value,
});
@Injectable()
export class RouterService {
/**
* The string value of the URL the user tried to navigate to while unauthenticated.
*
* Developed to allow users to deep link even when the navigation gets interrupted
* by the authentication process.
*/
private deepLinkRedirectUrlState: GlobalState<string>;
private previousUrl: string = undefined;
private currentUrl: string = undefined;
constructor(
private router: Router,
private activatedRoute: ActivatedRoute,
private titleService: Title,
private stateProvider: StateProvider,
i18nService: I18nService,
) {
this.deepLinkRedirectUrlState = this.stateProvider.getGlobal(DEEP_LINK_REDIRECT_URL);
this.currentUrl = this.router.url;
router.events
.pipe(filter((e) => e instanceof NavigationEnd))
.subscribe((event: NavigationEnd) => {
this.currentUrl = event.url;
let title = i18nService.t("bitWebVault");
if (this.currentUrl.includes("/sm/")) {
title = i18nService.t("bitSecretsManager");
}
let child = this.activatedRoute.firstChild;
while (child.firstChild) {
child = child.firstChild;
}
const titleId: string = child?.snapshot?.data?.titleId;
const rawTitle: string = child?.snapshot?.data?.title;
const updateUrl = !child?.snapshot?.data?.doNotSaveUrl ?? true;
if (titleId != null || rawTitle != null) {
const newTitle = rawTitle != null ? rawTitle : i18nService.t(titleId);
if (newTitle != null && newTitle !== "") {
title = newTitle + " | " + title;
}
}
this.titleService.setTitle(title);
if (updateUrl) {
this.setPreviousUrl(this.currentUrl);
}
});
}
getPreviousUrl(): string | undefined {
return this.previousUrl;
}
setPreviousUrl(url: string): void {
this.previousUrl = url;
}
/**
* Save URL to Global State. This service is used during the login process
* @param url URL being saved to the Global State
*/
async persistLoginRedirectUrl(url: string): Promise<void> {
await this.deepLinkRedirectUrlState.update(() => url);
}
/**
* Fetch and clear persisted LoginRedirectUrl if present in state
*/
async getAndClearLoginRedirectUrl(): Promise<string | undefined> {
const persistedPreLoginUrl = await firstValueFrom(this.deepLinkRedirectUrlState.state$);
if (!Utils.isNullOrEmpty(persistedPreLoginUrl)) {
await this.persistLoginRedirectUrl(null);
return persistedPreLoginUrl;
}
return;
}
}