import Vue from 'vue' import Router from 'vue-router' import DashboardPage from '@/components/DashboardPage' import dashboardCategories from '@/config/dashboard' import StartPage from '@/components/StartPage' function setChildren (routes, parentName) { routes.forEach(route => { if (route.name && parentName) route.name = parentName + '.' + route.name else if (!parentName) route.name = route.component.name if (route.component && route.component.routes) { route.children = setChildren(route.component.routes(), route.name) } }) return routes } Vue.use(Router) var router = new Router({ mode: 'history', routes: [ { path: '/', name: 'start', component: StartPage, children: StartPage.routes() }, { path: '/dashboard', name: 'dashboard', redirect: '/dashboard/home', component: DashboardPage, children: setChildren(DashboardPage.routes().concat(...dashboardCategories.map(c => c.modules))) }, { path: '*', redirect: '/dashboard/home' } ] }) const registerRouterGuards = function (store) { router.beforeEach((to, from, next) => { const loggedIn = document.cookie.indexOf('jwt_hp') >= 0 if (to.name === 'login') { if (loggedIn) next({ name: 'dashboard' }) else next() } else if (to.name === 'start') { if (loggedIn) next({ name: 'dashboard' }) else next({ name: 'login' }) } else if (to.name === 'setup') { if (loggedIn) next({ name: 'dashboard' }) else next() } else if (!loggedIn) { store.commit('setLoginRedirect', to.fullPath) next({ name: 'login' }) } else { next() } }) } export { router, registerRouterGuards }