@vincent15000 Would you not be better off just intercepting error responses? If you hit a page and it returns a 401 Unauthorized response then you can display a modal or something in your SPA telling the user they’re logged out, and to try logging in again.
Sep 30, 2023
12
Level 63
Vue router - beforeEach
Hello,
I have this code.
i'm trouble with how persist the connexion if the user refreshes manually the browser.
Is it a good idea to do what I have put in comments at the beginning of the callback inside the beforeEach guard ?
I have found this idea on the web, but I don't think that it's a good idea.
router.beforeEach((to) => {
// check if authStore.authUser is null
// if null, try to get user
// if I get user, then the user is connected
// if I don't get user, then the user isn't connected
if (to.meta.requiresAuth && !authStore.authUser) {
return { name: 'login' }
}
if (to.name == 'login' && authStore.authUser) {
return { name: 'dashboard' }
}
if (to.meta.notfound) {
if (authStore.authUser) {
return { name: 'dashboard' }
} else {
return { name: 'home' }
}
}
})
Where would you suggest me to put this code ?
In the App.vue file ?
In fact I think that it has to be in a file that is loaded only once when the browser loads the root view.
// check if authStore.authUser is null
// if null, try to get user
// if I get user, then the user is connected
// if I don't get user, then the user isn't connected
Thanks a lot for your suggestions.
V
Please or to participate in this conversation.