2
I’m using the Vue Rotuer
to control the routes of my project, with this, I saw the need to use Navigation Guards
.
<script>
export default{
beforeRouteEnter(to, from, next){
if(to.meta.adminOnly === false){
//alert('f')
next()
}
}
}
</script>
Up to there everything great, however, I saw a difficulty, because it is a dashboard
, I have the page of login
and other pages. On my page app.vue
I have the following structure.
<template>
<div id="app">
<Menu></Menu>
<router-view class="containerView"></router-view>
</div>
</template>
The component Menu
has all my side and top menu, because I want to change the page, it remains and only the contents change, so I left out the <router-view></router-view>
.
However, I am having difficulty to hide it, in case my user is not logged in (is on the login page), and I could not solve, I tried to use the beforeRouterEnter
on the page app.vue
to validate which page the user is in and whether the page contains the meta adminOnly
, but it didn’t work, and I didn’t want to use localStorage
to make this validation, because the same, would keep my system insecure of more.
You can’t make an ajax call inside the
beforeRouteEnter
to check if the session is valid? Maybe I didn’t get it right, but the check is against the right server?– Sergio
@Sergio I will still do this validation with the server, but even so, how would I hide my
Menu
:? and the callajax
, would stay in eachbeforeRouteEnter
?– Rafael Augusto
You can have a/flag variable that stores the state in memory. You would have to ajax once. Then you might want to have an inactivity timeout logic, but the flag would work per session. It looks like you want it to?
– Sergio
Yes, but how would it work? How would I create a
sessão
? and thatsessão
i could run from any of my files? and if the user updates the page for some reason, would remain logged in?– Rafael Augusto