Answer the question
In order to leave comments, you need to log in
How to make vue.js authorization check delay?
There is a SPA application on Vue.js + Laravel. All authorization logic resides directly in Laravel. When using Vue-router, there was a need to check if the user is logged in or not. Actually there is a small Auth class that does this.
export default {
user: {
authenticated : false
},
check: function(context) {
context.$http.get('/api/v1/user').then((response) => {
if (response.body.user != null) {
this.user.authenticated = true
}
}, (response) =>{
сonsole.log(response)
});
}
}
beforeRouteEnter (to, from, next) {
next(vm =>{
Auth.check(vm);
if (!Auth.user.authenticated) {
next({path:'/login'});
}
})
}
Answer the question
In order to leave comments, you need to log in
Thought it out at all. I underestimated the power of promises. Who cares, you need to return the promise
check: function(context) {
return context.$http.get('/api/v1/user').then((response) => {
if (response.body.user != null) {
this.user.authenticated = true
}
}, (response) =>{
сonsole.log(response)
});
}
beforeRouteEnter (to, from, next) {
Auth.check().then( ( ) => {
if(!Auth.user.authenticated)
next({ path: '/login' })
else
next()
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question