From 7d8d0b4d2b1286cb8b6a05737050d0c4c00420b3 Mon Sep 17 00:00:00 2001 From: PascalMarchesseault <97350299+PascalMarchesseault@users.noreply.github.com> Date: Mon, 8 Jul 2024 00:06:03 -0400 Subject: [PATCH] I added a redirect when we are on the wallet and profile pages and we are not logged in. --- src/router/index.js | 47 +++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 1ab1302..2ff1d06 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -1,4 +1,5 @@ -import {createRouter, createWebHistory} from 'vue-router' +import { createRouter, createWebHistory } from 'vue-router' +import { useUserStore } from "@/stores/user.js"; import GuillaumeAime from '@/views/manualusers/GuillaumeAime.vue' import About from '@/views/documentation/About.vue' import ContentPolicy from '@/views/documentation/ContentPolicy.vue' @@ -26,7 +27,7 @@ const routes = [ { path: '/', component: Home, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/browse', @@ -65,53 +66,53 @@ const routes = [ path: '/@mathieucaron', component: MathieuCaron }, - + { path: '/helpandcontact', component: HelpAndContact, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/termsandconditions', name: 'termsandconditions', component: TermsAndConditions, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/contentpolicy', name: 'contentpolicy', component: ContentPolicy, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/faq', name: 'FAQ', component: FAQ, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/guideforcreators', name: 'guideforcreators', component: CreatorGuide, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/about', name: 'about', component: About, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/pricing', name: 'pricing', component: Pricing, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { path: '/join', name: 'join', component: Join, - meta: {hideSideBar: true} + meta: { hideSideBar: true } }, { @@ -122,7 +123,8 @@ const routes = [ { path: '/profile', name: 'profile', - component: Profile + component: Profile, + meta: { requiresAuth: true } }, { path: '/signup', @@ -137,7 +139,8 @@ const routes = [ { path: '/wallet', name: 'wallet', - component: Wallet + component: Wallet, + meta: { requiresAuth: true } } ] @@ -146,4 +149,20 @@ const router = createRouter({ routes }) -export default router +// Navigation gards +router.beforeEach((to, from, next) => { + const authStore = useUserStore(); + + + if (to.matched.some(record => record.meta.requiresAuth)) { + if (!authStore.user.value || !Object.keys(authStore.user.value).length) { + next('/'); + } else { + next(); + } + } else { + next(); + } +}) + +export default router;