2018-06-10 04:40:53 +02:00
|
|
|
import { Injectable } from "@angular/core";
|
2018-07-06 04:37:35 +02:00
|
|
|
import { Title } from "@angular/platform-browser";
|
2018-06-10 04:40:53 +02:00
|
|
|
import { ActivatedRoute, NavigationEnd, Router } from "@angular/router";
|
2022-04-25 15:41:44 +02:00
|
|
|
import { filter } from "rxjs";
|
2018-06-10 04:40:53 +02:00
|
|
|
|
2023-06-06 22:34:53 +02:00
|
|
|
import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service";
|
2023-11-22 17:54:12 +01:00
|
|
|
import { StateService } from "@bitwarden/common/platform/abstractions/state.service";
|
|
|
|
import { Utils } from "@bitwarden/common/platform/misc/utils";
|
2018-07-06 04:37:35 +02:00
|
|
|
|
2018-06-10 04:40:53 +02:00
|
|
|
@Injectable()
|
|
|
|
export class RouterService {
|
|
|
|
private previousUrl: string = undefined;
|
|
|
|
private currentUrl: string = undefined;
|
|
|
|
|
2018-07-06 04:37:35 +02:00
|
|
|
constructor(
|
|
|
|
private router: Router,
|
|
|
|
private activatedRoute: ActivatedRoute,
|
2019-02-22 19:17:10 +01:00
|
|
|
private titleService: Title,
|
2023-11-22 17:54:12 +01:00
|
|
|
private stateService: StateService,
|
2019-02-22 19:17:10 +01:00
|
|
|
i18nService: I18nService
|
|
|
|
) {
|
2018-06-10 04:40:53 +02:00
|
|
|
this.currentUrl = this.router.url;
|
2022-04-25 15:41:44 +02:00
|
|
|
|
|
|
|
router.events
|
|
|
|
.pipe(filter((e) => e instanceof NavigationEnd))
|
|
|
|
.subscribe((event: NavigationEnd) => {
|
2018-06-10 04:40:53 +02:00
|
|
|
this.currentUrl = event.url;
|
2018-07-06 04:37:35 +02:00
|
|
|
|
2023-07-27 02:53:09 +02:00
|
|
|
let title = i18nService.t("bitWebVault");
|
|
|
|
|
|
|
|
if (this.currentUrl.includes("/sm/")) {
|
|
|
|
title = i18nService.t("bitSecretsManager");
|
|
|
|
}
|
|
|
|
|
2018-07-06 04:37:35 +02:00
|
|
|
let child = this.activatedRoute.firstChild;
|
2022-04-25 15:41:44 +02:00
|
|
|
while (child.firstChild) {
|
|
|
|
child = child.firstChild;
|
2018-07-06 04:37:35 +02:00
|
|
|
}
|
|
|
|
|
2022-04-25 15:41:44 +02:00
|
|
|
const titleId: string = child?.snapshot?.data?.titleId;
|
|
|
|
const rawTitle: string = child?.snapshot?.data?.title;
|
|
|
|
const updateUrl = !child?.snapshot?.data?.doNotSaveUrl ?? true;
|
|
|
|
|
2018-07-06 04:37:35 +02:00
|
|
|
if (titleId != null || rawTitle != null) {
|
|
|
|
const newTitle = rawTitle != null ? rawTitle : i18nService.t(titleId);
|
|
|
|
if (newTitle != null && newTitle !== "") {
|
|
|
|
title = newTitle + " | " + title;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.titleService.setTitle(title);
|
2022-04-25 15:41:44 +02:00
|
|
|
if (updateUrl) {
|
|
|
|
this.setPreviousUrl(this.currentUrl);
|
|
|
|
}
|
|
|
|
});
|
2018-06-10 04:40:53 +02:00
|
|
|
}
|
|
|
|
|
2023-11-22 17:54:12 +01:00
|
|
|
getPreviousUrl(): string | undefined {
|
2018-06-10 04:40:53 +02:00
|
|
|
return this.previousUrl;
|
|
|
|
}
|
2019-02-22 19:17:10 +01:00
|
|
|
|
2023-11-22 17:54:12 +01:00
|
|
|
setPreviousUrl(url: string): void {
|
2019-02-22 19:17:10 +01:00
|
|
|
this.previousUrl = url;
|
|
|
|
}
|
2023-11-22 17:54:12 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.stateService.setDeepLinkRedirectUrl(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch and clear persisted LoginRedirectUrl if present in state
|
|
|
|
*/
|
|
|
|
async getAndClearLoginRedirectUrl(): Promise<string> | undefined {
|
|
|
|
const persistedPreLoginUrl = await this.stateService.getDeepLinkRedirectUrl();
|
|
|
|
|
|
|
|
if (!Utils.isNullOrEmpty(persistedPreLoginUrl)) {
|
|
|
|
await this.persistLoginRedirectUrl(null);
|
|
|
|
return persistedPreLoginUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2018-06-10 04:40:53 +02:00
|
|
|
}
|