import Vue from 'vue' import Router from 'vue-router' import Login from '@/components/Login' import Main from '@/components/Main' import auth from '@/utils/auth.js' Vue.use(Router) const router = new Router({ base: document.querySelector('meta[name="base"]').getAttribute('content'), mode: 'history', routes: [ { path: '/login', name: 'Login', component: Login, beforeEnter: function (to, from, next) { auth.loggedIn() .then(() => { next({ path: '/files' }) }) .catch(() => { document.title = 'Login' next() }) } }, { path: '/*', component: Main, meta: { requiresAuth: true }, children: [ { path: '/files*', name: 'Files' }, { path: '/*', redirect: { name: 'Files' } } ] } ] }) router.beforeEach((to, from, next) => { if (to.matched.some(record => record.meta.requiresAuth)) { // this route requires auth, check if logged in // if not, redirect to login page. auth.loggedIn() .then(() => { next() }) .catch(e => { next({ path: '/login', query: { redirect: to.fullPath } }) }) return } next() }) export default router