Created
August 23, 2019 12:34
-
-
Save mccabiles/76b1aea3ad2724978c24f339825ae9d2 to your computer and use it in GitHub Desktop.
Vue Router checking for authenticated
This file contains hidden or 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'; // Vuex.store | |
// Check for auth routes: | |
const guardAuthRoutes = (to, from, next) => { | |
const matchedRoutes = to.matched; | |
const isLoggedIn = Store.state.Auth.isLoggedIn; | |
if (matchedRoutes.some(route => route.meta.auth === true) && !isLoggedIn) { | |
return next({ name: 'login' }); | |
} | |
next(); | |
} | |
// Check for guest routes: | |
const guardGuestRoutes = (to, from, next) => { | |
const matchedRoutes = to.matched; | |
const isLoggedIn = Store.state.Auth.isLoggedIn; | |
if (matchedRoutes.some(route => route.meta.guest === true) && isLoggedIn) { | |
return next({ name: 'home' }); | |
} | |
next(); | |
}; | |
router.beforeEach(guardGuestRoutes); | |
router.beforeEach(guardAuthRoutes); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment