Last active
January 30, 2018 09:54
-
-
Save vouill/86a14f8d656a0f2c43b909c90945fd70 to your computer and use it in GitHub Desktop.
router auth
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 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