From 4a42ad69ae6ccf79f58672335e1f21bf92923e60 Mon Sep 17 00:00:00 2001 From: Francis Pion Date: Wed, 20 Dec 2023 11:26:51 -0500 Subject: [PATCH] Implemented navigation guard. --- src/router.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/router.ts b/src/router.ts index af7f7c2..038cdac 100644 --- a/src/router.ts +++ b/src/router.ts @@ -1,5 +1,7 @@ import { createRouter, createWebHistory } from "vue-router"; + import HomeView from "@/views/HomeView.vue"; +import { useAccountStore } from "./stores/account"; const router = createRouter({ history: createWebHistory(import.meta.env.BASE_URL), @@ -8,6 +10,7 @@ const router = createRouter({ path: "/", name: "Home", component: HomeView, + meta: { isPublic: true }, }, // Account { @@ -17,6 +20,7 @@ const router = createRouter({ // this generates a separate chunk (About.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () => import("@/views/account/ConfirmView.vue"), + meta: { isPublic: true }, }, { path: "/profile", @@ -27,11 +31,13 @@ const router = createRouter({ path: "/register", name: "Register", component: () => import("@/views/account/RegisterView.vue"), + meta: { isPublic: true }, }, { path: "/sign-in", name: "SignIn", component: () => import("@/views/account/SignInView.vue"), + meta: { isPublic: true }, }, { path: "/sign-out", @@ -41,4 +47,11 @@ const router = createRouter({ ], }); +router.beforeEach((to) => { + const account = useAccountStore(); + if (!to.meta.isPublic && !account.authenticated) { + return { name: "SignIn", query: { redirect: to.fullPath } }; + } +}); + export default router;