This commit is contained in:
2020-11-26 01:30:15 +01:00
parent bd6f4f3827
commit 53e8aa9a9c
17 changed files with 566 additions and 111 deletions

View File

@@ -1,6 +1,10 @@
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Home from '@/views/Home.vue'
import Login from "@/views/Login";
import store from '@/store'
Vue.use(VueRouter)
@@ -8,7 +12,12 @@ const routes = [
{
path: '/',
name: 'Home',
component: Home
component: Home,
meta: {
allowVisit(authorized) {
return authorized;
}
}
},
{
path: '/about',
@@ -16,7 +25,22 @@ const routes = [
// 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')
component: () => import(/* webpackChunkName: "about" */ '../views/About.vue'),
meta: {
allowVisit() {
return true;
}
}
},
{
path: '/login',
name: 'Login',
component: Login,
meta: {
allowVisit(authorized) {
return !authorized;
}
}
}
]
@@ -26,4 +50,24 @@ const router = new VueRouter({
routes
})
router.beforeEach((to, from, next) => {
const authorized = store.getters.isLoggedIn;
const visitAllowed = to.matched.some(record => record.meta.allowVisit(authorized))
if (visitAllowed) {
next();
return;
}
if (authorized) {
next({name: 'Home'})
} else {
next({name: 'Login'})
}
})
export default router