If the user/visitor has access to the API route, then the status code will 200, else 401. I know that I can user Vue navigation guards, but I will need to use Vuex.
This way is much simpler and faster, what do you think, is it a right way to protect Vue page from unauthenticated users?
<template>
<v-container>
<div v-if="isAuthenticated === false">loading</div>
<div v-else>Protected Profile Page</div>
</v-container>
</template>
<script>
export default {
data () {
return {
isAuthenticated : false,
}
},
beforeCreate: async function() {
axios.get('api/user', {})
.then(res => {
this.isAuthenticated = true;
}).catch(err => {
this.isAuthenticated = false;
this.$router.push('/login');
})
},
created () {
console.log('isAuthenticated', this.isAuthenticated) // return false
}
}
</script>