Last active
May 5, 2021 15:52
-
-
Save susanBuck/4b04c2a3679f42dde7011f4f9cec5466 to your computer and use it in GitHub Desktop.
ZipFoods router.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createRouter, createWebHistory } from 'vue-router'; | |
import { store } from '@/common/store'; | |
// Define all the routes of our application | |
const routes = [ | |
{ | |
path: '/', | |
// Rather than having to have separate import statements at the top of this page for each component | |
// Here’s a simple way we can directly make our components available | |
component: () => import('@/components/pages/HomePage.vue'), | |
}, | |
{ | |
path: '/products', | |
component: () => import('@/components/pages/ProductsPage.vue'), | |
}, | |
{ | |
path: '/product/new', | |
component: () => import('@/components/pages/ProductCreatePage.vue'), | |
// Note the addition of this meta attribute: | |
meta: { | |
requiresAuth: true | |
} | |
}, | |
{ | |
path: '/product/:id', | |
component: () => import('@/components/pages/ProductPage.vue'), | |
props: true, | |
}, | |
{ | |
path: '/categories', | |
component: () => import('@/components/pages/CategoriesPage.vue'), | |
}, | |
{ | |
path: '/cart', | |
component: () => import('@/components/pages/CartPage.vue'), | |
}, | |
{ | |
path: '/account', | |
component: () => import('@/components/pages/AccountPage.vue'), | |
}, | |
{ | |
// This is a route we can direct the user to if they try to access | |
// a part of the site they don’t have privileges for | |
path: '/denied', | |
component: () => import('@/components/pages/AccessDeniedPage.vue'), | |
}, | |
]; | |
// Initialize our router | |
const router = createRouter({ | |
history: createWebHistory(), | |
routes, | |
}); | |
// beforeEach provides a way to execute some code before a route is resolved | |
// Ref: https://router.vuejs.org/guide/advanced/navigation-guards.html | |
router.beforeEach(async (to) => { | |
// Exact the meta information from our routes | |
// Ref: https://router.vuejs.org/guide/advanced/meta.html#route-meta-fields | |
const requiresAuth = to.matched.some(record => record.meta.requiresAuth); | |
// Put the “decide what to do” code into a method so we can wait to invoke it | |
// once we have the user data | |
const decide = (user) => { | |
if (requiresAuth && !user) { | |
// If they’re trying to access a requiresAuth route and they’re not logged in, | |
// they get sent to “Access Denied” page | |
return '/denied'; | |
} else { | |
// Otherwise, allow them to intended destination | |
return true; | |
} | |
} | |
// If we don’t have the user yet, dispatch our Vuex authUser action | |
if (store.state.user === null) { | |
// Observe how this code expects our authUser action to be asynchronous | |
let authUser = await store.dispatch('authUser'); | |
if (authUser !== null) { | |
return decide(store.state.user); | |
} | |
} else { | |
return decide(store.state.user); | |
} | |
}); | |
export { router }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment