Hide component outside the router-Viewer

Asked

Viewed 255 times

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 I will still do this validation with the server, but even so, how would I hide my Menu:? and the call ajax, would stay in each beforeRouteEnter?

  • 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?

  • Yes, but how would it work? How would I create a sessão? and that sessão i could run from any of my files? and if the user updates the page for some reason, would remain logged in?

1 answer

1


The tools available on the Vue-router are:

  • beforeEach
  • fields meta

Configure each route that needs authentication with meta: { requiresAuth: true }. You can put that on children as is in the documentation:

const router = new VueRouter({
  routes: [
    {
      path: '/foo',
      component: Foo,
      children: [
        {
          path: 'bar',
          component: Bar,
          // a meta field
          meta: { requiresAuth: true }
        }
      ]
    }
  ]
})

Then you check it before each route. The beforeEach passes the to which is the route that was requested. Then you can check if that route needs authorization. If you need to and are not logged in, redirect to the login page.

router.beforeEach((to, from, next) => {
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // this route requires auth, check if logged in
    // if not, redirect to login page.
    if (!auth.loggedIn()) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    next() // make sure to always call next()!
  }
})

The part of saving the information of being logged in or there are no different ways. You can save only in a variable with status true|false, via localStorage etc..

There is a video about this useful: https://www.youtube.com/watch?v=i2yc_ACcMA0

  • Sergio, forgive me my ignorance, but what is variavel de estado? I think in localStorage, it is vulnerable, because a person can go and put there as true, there is access to the whole system.

  • @Rafaelaugusto don’t mind not knowing. That’s why the site exists. I’m sorry I can’t respond faster. So the idea of this state variable is in its simplest version something like let autenticado = true;. Very simple. In Javascript you can never guarantee that the application is not manipulated, so I suggest that the server has session control to not send data to the client if the login has not been done. You could use Lazy-loading if components have to have data when loading https://router.vuejs.org/en/advanced/lazy-loading.html

  • Sergio, what is this path and component ?

  • Sergio, I could understand now, thanks for the help, I think it made me understand already how to hide the component, and understand even more the structure of vue, now validation is with me, hugs

  • @Rafaelaugusto great. I’m still at work so I couldn’t answer, but I’m glad you found out. If you have any more questions, ask me and I’ll help you when I can.

  • Sergio, I ask you a question (if you think I should post a question for this, you can say I post), following what you told me, my Menu is a component, how would you pass a value to him? and I call my component Menu in the App.vue and not in the Dashboard.vue

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.