diff options
Diffstat (limited to 'front-end/src/router')
-rw-r--r-- | front-end/src/router/index.ts | 70 |
1 files changed, 68 insertions, 2 deletions
diff --git a/front-end/src/router/index.ts b/front-end/src/router/index.ts index 538c132..4238621 100644 --- a/front-end/src/router/index.ts +++ b/front-end/src/router/index.ts @@ -1,5 +1,71 @@ +import { watch } from 'vue'; +import { useSession } from '@vnuge/vnlib.browser'; +import { useSessionStorage, get, set } from '@vueuse/core'; +import { includes, map, toLower } from 'lodash-es'; +import { type Router } from 'vue-router'; + import { createRouter, createWebHistory } from 'vue-router/auto' +import { routes } from 'vue-router/auto-routes' export default createRouter({ - history: createWebHistory(import.meta.env.BASE_URL) -})
\ No newline at end of file + history: createWebHistory(import.meta.env.BASE_URL), + routes +}) + +/** + * Enables page guards for protected routes and configures a + * last page store. + */ +export const guardRoutes = (router: Router, protectedRoutes: string[]) => { + const { loggedIn } = useSession() + + const lastPageStore = useSessionStorage('lastPageStore', undefined) + + const { beforeEach, currentRoute, afterEach, push } = router + + //Convert routes to lowercase + protectedRoutes = map(protectedRoutes, toLower); + + //Setup nav guards + beforeEach((to, from) => { + if (!to.name) { + return true; + } + + if (!get(loggedIn)) { + if (includes(protectedRoutes, toLower(to.name as string))) { + + //Set last page as from page + set(lastPageStore, from.fullPath) + + return { name: 'Login' } + } + } + else { + /** + * If the user is going back to the login page, are logged in, + * and have a previous page to go back to, redirect to the last page + * instead of the login page + */ + const lastPath = get(lastPageStore); + + if (to.name === 'Login' && lastPath) { + set(lastPageStore, undefined) //Clear the last page + return lastPath ? { path: lastPath } : true; + } + } + + //Allow + return true; + }) + + //scroll window back to top + afterEach(() => window.scrollTo(0, 0)) + + watch(loggedIn, (li) => { + //If the user gets logged out, redirect to login + if (li === false && currentRoute.value.name !== 'Login') { + push({ name: 'Login' }) + } + }) +}
\ No newline at end of file |