aboutsummaryrefslogtreecommitdiff
path: root/lib/vnlib.browser/src/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vnlib.browser/src/helpers')
-rw-r--r--lib/vnlib.browser/src/helpers/lastPage.ts25
-rw-r--r--lib/vnlib.browser/src/helpers/pageGuard.ts12
2 files changed, 27 insertions, 10 deletions
diff --git a/lib/vnlib.browser/src/helpers/lastPage.ts b/lib/vnlib.browser/src/helpers/lastPage.ts
index 8be2116..b3c7103 100644
--- a/lib/vnlib.browser/src/helpers/lastPage.ts
+++ b/lib/vnlib.browser/src/helpers/lastPage.ts
@@ -1,4 +1,4 @@
-// Copyright (c) 2023 Vaughn Nugent
+// Copyright (c) 2024 Vaughn Nugent
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
@@ -50,6 +50,19 @@ export interface ILastPageStorage {
pop(): string | null;
}
+/**
+ * Represents a router-like object that can be used
+ * to navigate to a desired route
+ */
+export interface RouterLike{
+ currentRoute: {
+ value: {
+ fullPath: string;
+ }
+ }
+ push(route: object | string): void;
+}
+
const storageKey = "lastPage";
//Storage impl
@@ -79,29 +92,29 @@ const defaultStack = (): ILastPageStorage => {
* last page after login.
* @returns { gotoLastPage: Function }
*/
-export const useLastPage = (storage?: ILastPageStorage): ILastPage => {
+export const useLastPage = (storage?: ILastPageStorage, router?: RouterLike): ILastPage => {
//fallback to default storage
const _storage = defaultTo(storage, defaultStack());
//Get the current router instance
- const router = useRouter();
+ const _router = defaultTo(router, useRouter());
//Store the current page to the last page stack
- const push = () => _storage.push(router.currentRoute.value.fullPath);
+ const push = () => _storage.push(_router.currentRoute.value.fullPath);
const pushAndNavigate = (route: object) => {
//Store the current page to the last page stack
push();
//Navigate to the desired route
- router.push(route);
+ _router.push(route);
};
const gotoLastPage = () => {
//Get the last stored page and navigate to it
const lp = _storage.pop();
if (lp) {
- router.push(lp);
+ _router.push(lp);
}
};
diff --git a/lib/vnlib.browser/src/helpers/pageGuard.ts b/lib/vnlib.browser/src/helpers/pageGuard.ts
index c3a767b..e6a92c6 100644
--- a/lib/vnlib.browser/src/helpers/pageGuard.ts
+++ b/lib/vnlib.browser/src/helpers/pageGuard.ts
@@ -1,4 +1,4 @@
-// Copyright (c) 2023 Vaughn Nugent
+// Copyright (c) 2024 Vaughn Nugent
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
@@ -20,8 +20,12 @@
import { watch } from 'vue'
import { useSession } from "../session";
import { useLastPage } from './lastPage';
-import type { ILastPageStorage } from "./lastPage";
+import type { ILastPageStorage, RouterLike } from "./lastPage";
+export interface PageGuardOptions {
+ readonly stack: ILastPageStorage;
+ readonly router: RouterLike;
+}
/**
* When called, configures the component to
@@ -30,12 +34,12 @@ import type { ILastPageStorage } from "./lastPage";
* @remarks Once called, if the user is logged-in changes will be
* watch to redirect if the user becomes logged out.
*/
-export const usePageGuard = (loginRoute = { name: 'Login' }, lpStorage?: ILastPageStorage): void => {
+export const usePageGuard = (loginRoute = { name: 'Login' }, options?: Partial<PageGuardOptions>): void => {
//Get the session state
const session = useSession();
//Get last page controller, fall back to default session storage stack
- const { pushAndNavigate } = useLastPage(lpStorage)
+ const { pushAndNavigate } = useLastPage(options?.stack, options?.router)
// Initial check for logged in to guard the page
if (!session.loggedIn.value) {