I'm very new to Nuxt and i'm working on a SSR app that uses Django on the backend.
I'm trying to create the login page, and before rendering it i would like to check if the user is already logged in, and if it is redirect the user to another page.
For various reasons i cannot do this from a Nuxt middleware, since i need this code to be executed from client side and not from server side, so i decided to use beforeRouteEnter
. My code seems to work, but the problem is that it will load the page and then, after more or less 1-2 seconds redirect to another page. This is a bit ugly, since i don't want the user to see the login page before being redirected.
I tried the same with beforeCreate
but the outcome was the same
Why does that happen? Is it because the page is cached? Can i fix it?
Here is the code:
export default {
name: 'LoginPage',
props: {
},
data(){
return {
}
},
beforeRouteEnter (to, from, next) {
return axios({
method: 'post',
url: 'http://127.0.0.1:8000/checkAuth',
data: {
},
withCredentials: true,
headers: {}
}).then(function (response) {
if (response['data']['state'] == 'True'){
//user is logged in, redirect
next('/')
} else {
//user is not logged in, don't redirect
next()
}
}).catch(function (error) {
next()
});
}
...
}