diff options
author | vnugent <public@vaughnnugent.com> | 2024-06-05 14:44:08 -0400 |
---|---|---|
committer | vnugent <public@vaughnnugent.com> | 2024-06-05 14:44:08 -0400 |
commit | 9eed4022a79f2cba139c9f8a359bfc8c1f9c31c5 (patch) | |
tree | 5bd97ad742dddf5ee488afd2fcd314efef9cf642 /front-end/src/router/index.ts | |
parent | f9e2109c27af5ece546261c018d4b2781860ff1c (diff) |
ci: Stage blocking changes
Diffstat (limited to 'front-end/src/router/index.ts')
-rw-r--r-- | front-end/src/router/index.ts | 66 |
1 files changed, 65 insertions, 1 deletions
diff --git a/front-end/src/router/index.ts b/front-end/src/router/index.ts index 538c132..2060965 100644 --- a/front-end/src/router/index.ts +++ b/front-end/src/router/index.ts @@ -1,5 +1,69 @@ +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' export default createRouter({ history: createWebHistory(import.meta.env.BASE_URL) -})
\ No newline at end of file +}) + +/** + * 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 |