import Vue from 'vue' import VueRouter from 'vue-router' import Collections from '@/views/Collections.vue' import Collection from "@/views/Collection"; import Item from "@/views/Item"; import Login from "@/views/Login"; import store from '@/store' Vue.use(VueRouter) const routes = [ { path: '/', name: 'Collections', component: Collections, meta: { allowVisit(authorized) { return authorized; } } }, { path: '/collection/:id', name: 'Collection', component: Collection, meta: { allowVisit(authorized) { return authorized; } } }, { path: '/item/:id', name: 'Item', component: Item, meta: { allowVisit(authorized) { return authorized; } } }, { path: '/about', name: 'About', // route level code-splitting // this generates a separate chunk (about.[hash].js) for this route // which is lazy-loaded when the route is visited. component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'), meta: { allowVisit() { return true; } } }, { path: '/login', name: 'Login', component: Login, meta: { allowVisit(authorized) { return !authorized; } } }, { path: '*', name: 'NotFound', component: () => import(/* webpackChunkName: "about" */ '../views/NotFound'), meta: { allowVisit() { return true; } } } ] const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes }) router.beforeEach((to, from, next) => { if (!store.state.appReady) { next(); return; } const authorized = store.getters.isLoggedIn; const visitAllowed = to.matched.some(record => record.meta.allowVisit(authorized)) if (visitAllowed) { next(); return; } if (authorized) { next({name: 'Collections'}) } else { next({name: 'Login'}) } }) export default router