Last active
April 18, 2018 09:24
-
-
Save michalbujalski/491add165fb207c90c544401161d5c46 to your computer and use it in GitHub Desktop.
Router setup for auth in Vue
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 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