Skip to content

Instantly share code, notes, and snippets.

@vouill
Last active January 30, 2018 09:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vouill/86a14f8d656a0f2c43b909c90945fd70 to your computer and use it in GitHub Desktop.
Save vouill/86a14f8d656a0f2c43b909c90945fd70 to your computer and use it in GitHub Desktop.
router auth
import store from '../store' // your vuex store
const ifNotAuthenticated = (to, from, next) => {
if (!store.getters.isAuthenticated) {
next()
return
}
next('/')
}
const ifAuthenticated = (to, from, next) => {
if (store.getters.isAuthenticated) {
next()
return
}
next('/login')
}
export default new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'Home',
component: Home,
},
{
path: '/account',
name: 'Account',
component: Account,
beforeEnter: ifAuthenticated,
},
{
path: '/login',
name: 'Login',
component: Login,
beforeEnter: ifNotAuthenticated,
},
],
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment