Created
March 24, 2019 18:51
-
-
Save igorkosta/7d5d6ed08bc399299d76c5aa94338d67 to your computer and use it in GitHub Desktop.
src/router/index.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 Vue from 'vue' | |
import Router from 'vue-router' | |
import store from '@/store/index.js' | |
import routes from '@/router/routes/index.js' | |
Vue.use(Router) | |
const router = new Router({ | |
routes: [ | |
{ | |
path: '/', | |
redirect: '/dashboard' | |
} | |
].concat(routes) | |
}) | |
router.beforeEach((to, from, next) => { | |
const authenticated = store.state.user.authenticated | |
const onlyLoggedOut = to.matched.some(record => record.meta.onlyLoggedOut) | |
const isPublic = to.matched.some(record => record.meta.public) | |
if (!isPublic && !authenticated) { | |
// this route requires auth, check if logged in | |
// if not, redirect to login page. | |
return next({ | |
path: '/login', | |
query: { redirect: to.fullPath } | |
}) | |
} | |
if (authenticated && onlyLoggedOut) { | |
return next('/') | |
} | |
next() | |
}) | |
export default router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment