Skip to content

Commit

Permalink
Implemented navigation guard.
Browse files Browse the repository at this point in the history
  • Loading branch information
Utar94 committed Dec 20, 2023
1 parent cdafdab commit 4a42ad6
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -8,6 +10,7 @@ const router = createRouter({
path: "/",
name: "Home",
component: HomeView,
meta: { isPublic: true },
},
// Account
{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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;

0 comments on commit 4a42ad6

Please sign in to comment.