Skip to content

Instantly share code, notes, and snippets.

@michalbujalski
Last active April 18, 2018 09:24
Show Gist options
  • Save michalbujalski/491add165fb207c90c544401161d5c46 to your computer and use it in GitHub Desktop.
Save michalbujalski/491add165fb207c90c544401161d5c46 to your computer and use it in GitHub Desktop.
Router setup for auth in Vue
import Vue from 'vue'
import Router from 'vue-router'
import Home from '../views/Home'
import SignIn from '../views/SignIn'
import SignOut from '../views/SignOut'
import Profile from '../views/Profile'
import store from '../store'
Vue.use(Router)
const router = new Router({
mode: 'history',
routes: [
{
path: '/',
name: 'home',
component: Home
},
{
path: '/signin',
name: 'signin',
component: SignIn
},
{
path: '/signout',
name: 'signout',
component: SignOut
},
{
path: '/profile',
name: 'profile',
component: Profile,
meta: {
authRequred: true
}
}
]
})
router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.authRequred)) {
if (!store.state.user) {
next({
path: '/signin',
query: { redirect: to.fullPath }
})
} else {
next()
}
} else {
next()
}
})
export default router
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment