Skip to content

Instantly share code, notes, and snippets.

@igorkosta
Created March 24, 2019 18:51
Show Gist options
  • Save igorkosta/7d5d6ed08bc399299d76c5aa94338d67 to your computer and use it in GitHub Desktop.
Save igorkosta/7d5d6ed08bc399299d76c5aa94338d67 to your computer and use it in GitHub Desktop.
src/router/index.js
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