Skip to content

Instantly share code, notes, and snippets.

@vouill

vouill/router.js

Last active Jan 30, 2018
Embed
What would you like to do?
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
You can’t perform that action at this time.