Skip to content

Instantly share code, notes, and snippets.

@vinicius73
Created February 19, 2019 19:31
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vinicius73/375648ba7002062ec57526cdcb142552 to your computer and use it in GitHub Desktop.
Save vinicius73/375648ba7002062ec57526cdcb142552 to your computer and use it in GitHub Desktop.
Vuex Route Guard DEMO
import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
import plugins from './plugins';
plugins({
router, store, Vue,
});
new Vue({
router,
store,
render: h => h(App),
}).$mount('#app');
const needAuth = (route) => {
const { requiresAuth } = route.meta;
if (requiresAuth === undefined) {
return true;
}
return requiresAuth;
};
const install = ({
store, router,
}) => {
router.beforeEach((to, from, next) => {
if (!needAuth(to)) {
next();
return;
}
store.dispatch('auth/checkUser')
.then(() => {
next();
})
.catch((e) => {
console.warn(e);
next({ name: 'Login' });
});
});
};
export default install;
import auth from './auth';
const install = (options) => {
[auth].forEach((plugin) => {
plugin(options);
});
};
export default install;
import Vue from 'vue';
import Router from 'vue-router';
import routes from './routes';
Vue.use(Router);
export default new Router({
base: process.env.BASE_URL,
routes,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment