I am building SPA and the problem is checking if user is admin or not.
After Vue.auth.getUserInfo()
I want to stop whole application and wait for API response, Vue.auth.user.isAdmin
is always false because I don't have response from api...
Here is router.beforeEach
router.beforeEach((to, from, next) => {
if(Vue.auth.user.authenticated == false) {
Vue.auth.getUserInfo();
}
if(Vue.auth.user.isAdmin) {
next({ name: 'admin.index' })
} else {
next({name: 'client.index'})
}
}
Get user info method:
getUserInfo() {
Vue.http.get('/api/me')
.then(({data}) => {
this.user = data;
}, () => {
this.logout();
})
}
Assuming the state of
Vue.auth.user.isAdmin
is managed within yourVue.auth.getUserInfo()
logic, you can try a promise approach (untested):Then, when you consume it in your guard (https://router.vuejs.org/en/advanced/navigation-guards.html):