Created
February 19, 2019 19:31
-
-
Save vinicius73/375648ba7002062ec57526cdcb142552 to your computer and use it in GitHub Desktop.
Vuex Route Guard DEMO
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 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'); |
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
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; |
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 auth from './auth'; | |
const install = (options) => { | |
[auth].forEach((plugin) => { | |
plugin(options); | |
}); | |
}; | |
export default install; |
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 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