Skip to content

Instantly share code, notes, and snippets.

@mccabiles
Created August 23, 2019 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mccabiles/76b1aea3ad2724978c24f339825ae9d2 to your computer and use it in GitHub Desktop.
Save mccabiles/76b1aea3ad2724978c24f339825ae9d2 to your computer and use it in GitHub Desktop.
Vue Router checking for authenticated
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