guard function on vue router only work on second loop












0















I am trying to protect the routes with a role-based guard, however when executing the routes, only execute the validation on the second route that I visit. This happens again and again, it seems to be a problem with the children routes, only validation is executed in the 2nd route children visit. I attach my code






router.beforeEach((to,from, next)=>{
const rolesToPath = (to.meta.roles)?to.meta.roles:null //roles authorized in next path
const reqAuth = to.matched.some(record=> record.meta.auth)
const currentUser = store.state.currentUser //current user With Roles
const homeRoute = '/dashboard'
const loginRoute = '/login'

if (reqAuth && currentUser) {
let auth = matchRoles(rolesToPath,currentUser.roles)
if (auth) {
next()
} else {
next(homeRoute)
}
}


function matchRoles(x,y){ //this function resolve if user is authorized
// to next path
let flag = false
x.forEach((x)=>{
y.forEach((y)=>{
if (x.userRol==y.name) {
flag = true//if role user is on next path
}
})
})

if (flag) return true
return false

}

})





These are the routes to which I want to apply the validation






import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

export default new VueRouter({
mode: 'history', // https://router.vuejs.org/api/#mode
linkActiveClass: 'open active',
scrollBehavior: () => ({ y: 0 }),
routes:[
{
path: '/login',
name: 'Login',
component: Login,
meta : {
auth: false,
},
},
{
path: '/register',
name: 'Register',
component: Register,
meta : {
auth: false,
},
},
{
path: '/',
redirect: '/dashboard',
name: 'Home',
component: DefaultContainer,
meta : {
auth: false,
},
children: [
{
path : 'credenciales',
redirect : '/credenciales/proveedores',
name : 'Credenciales',
component : {
render (c) { return c('router-view') }
},
children: [
{
path : 'proveedores',
name : 'Proveedores',
component : TablaProveedores,
meta : {
auth: true,
roles: [
{
userRol : 'tester',
lock : true,
}
]
},
},
{
path : 'accesos',
name : 'Accesos',
component : TablaProveedores,
meta : {
auth: true,
roles: [
{
userRol : 'admin',
lock : true,
}
]
},
},
]
},
]
})












share|improve this question



























    0















    I am trying to protect the routes with a role-based guard, however when executing the routes, only execute the validation on the second route that I visit. This happens again and again, it seems to be a problem with the children routes, only validation is executed in the 2nd route children visit. I attach my code






    router.beforeEach((to,from, next)=>{
    const rolesToPath = (to.meta.roles)?to.meta.roles:null //roles authorized in next path
    const reqAuth = to.matched.some(record=> record.meta.auth)
    const currentUser = store.state.currentUser //current user With Roles
    const homeRoute = '/dashboard'
    const loginRoute = '/login'

    if (reqAuth && currentUser) {
    let auth = matchRoles(rolesToPath,currentUser.roles)
    if (auth) {
    next()
    } else {
    next(homeRoute)
    }
    }


    function matchRoles(x,y){ //this function resolve if user is authorized
    // to next path
    let flag = false
    x.forEach((x)=>{
    y.forEach((y)=>{
    if (x.userRol==y.name) {
    flag = true//if role user is on next path
    }
    })
    })

    if (flag) return true
    return false

    }

    })





    These are the routes to which I want to apply the validation






    import Vue from 'vue'
    import VueRouter from 'vue-router'
    Vue.use(VueRouter)

    export default new VueRouter({
    mode: 'history', // https://router.vuejs.org/api/#mode
    linkActiveClass: 'open active',
    scrollBehavior: () => ({ y: 0 }),
    routes:[
    {
    path: '/login',
    name: 'Login',
    component: Login,
    meta : {
    auth: false,
    },
    },
    {
    path: '/register',
    name: 'Register',
    component: Register,
    meta : {
    auth: false,
    },
    },
    {
    path: '/',
    redirect: '/dashboard',
    name: 'Home',
    component: DefaultContainer,
    meta : {
    auth: false,
    },
    children: [
    {
    path : 'credenciales',
    redirect : '/credenciales/proveedores',
    name : 'Credenciales',
    component : {
    render (c) { return c('router-view') }
    },
    children: [
    {
    path : 'proveedores',
    name : 'Proveedores',
    component : TablaProveedores,
    meta : {
    auth: true,
    roles: [
    {
    userRol : 'tester',
    lock : true,
    }
    ]
    },
    },
    {
    path : 'accesos',
    name : 'Accesos',
    component : TablaProveedores,
    meta : {
    auth: true,
    roles: [
    {
    userRol : 'admin',
    lock : true,
    }
    ]
    },
    },
    ]
    },
    ]
    })












    share|improve this question

























      0












      0








      0








      I am trying to protect the routes with a role-based guard, however when executing the routes, only execute the validation on the second route that I visit. This happens again and again, it seems to be a problem with the children routes, only validation is executed in the 2nd route children visit. I attach my code






      router.beforeEach((to,from, next)=>{
      const rolesToPath = (to.meta.roles)?to.meta.roles:null //roles authorized in next path
      const reqAuth = to.matched.some(record=> record.meta.auth)
      const currentUser = store.state.currentUser //current user With Roles
      const homeRoute = '/dashboard'
      const loginRoute = '/login'

      if (reqAuth && currentUser) {
      let auth = matchRoles(rolesToPath,currentUser.roles)
      if (auth) {
      next()
      } else {
      next(homeRoute)
      }
      }


      function matchRoles(x,y){ //this function resolve if user is authorized
      // to next path
      let flag = false
      x.forEach((x)=>{
      y.forEach((y)=>{
      if (x.userRol==y.name) {
      flag = true//if role user is on next path
      }
      })
      })

      if (flag) return true
      return false

      }

      })





      These are the routes to which I want to apply the validation






      import Vue from 'vue'
      import VueRouter from 'vue-router'
      Vue.use(VueRouter)

      export default new VueRouter({
      mode: 'history', // https://router.vuejs.org/api/#mode
      linkActiveClass: 'open active',
      scrollBehavior: () => ({ y: 0 }),
      routes:[
      {
      path: '/login',
      name: 'Login',
      component: Login,
      meta : {
      auth: false,
      },
      },
      {
      path: '/register',
      name: 'Register',
      component: Register,
      meta : {
      auth: false,
      },
      },
      {
      path: '/',
      redirect: '/dashboard',
      name: 'Home',
      component: DefaultContainer,
      meta : {
      auth: false,
      },
      children: [
      {
      path : 'credenciales',
      redirect : '/credenciales/proveedores',
      name : 'Credenciales',
      component : {
      render (c) { return c('router-view') }
      },
      children: [
      {
      path : 'proveedores',
      name : 'Proveedores',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'tester',
      lock : true,
      }
      ]
      },
      },
      {
      path : 'accesos',
      name : 'Accesos',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'admin',
      lock : true,
      }
      ]
      },
      },
      ]
      },
      ]
      })












      share|improve this question














      I am trying to protect the routes with a role-based guard, however when executing the routes, only execute the validation on the second route that I visit. This happens again and again, it seems to be a problem with the children routes, only validation is executed in the 2nd route children visit. I attach my code






      router.beforeEach((to,from, next)=>{
      const rolesToPath = (to.meta.roles)?to.meta.roles:null //roles authorized in next path
      const reqAuth = to.matched.some(record=> record.meta.auth)
      const currentUser = store.state.currentUser //current user With Roles
      const homeRoute = '/dashboard'
      const loginRoute = '/login'

      if (reqAuth && currentUser) {
      let auth = matchRoles(rolesToPath,currentUser.roles)
      if (auth) {
      next()
      } else {
      next(homeRoute)
      }
      }


      function matchRoles(x,y){ //this function resolve if user is authorized
      // to next path
      let flag = false
      x.forEach((x)=>{
      y.forEach((y)=>{
      if (x.userRol==y.name) {
      flag = true//if role user is on next path
      }
      })
      })

      if (flag) return true
      return false

      }

      })





      These are the routes to which I want to apply the validation






      import Vue from 'vue'
      import VueRouter from 'vue-router'
      Vue.use(VueRouter)

      export default new VueRouter({
      mode: 'history', // https://router.vuejs.org/api/#mode
      linkActiveClass: 'open active',
      scrollBehavior: () => ({ y: 0 }),
      routes:[
      {
      path: '/login',
      name: 'Login',
      component: Login,
      meta : {
      auth: false,
      },
      },
      {
      path: '/register',
      name: 'Register',
      component: Register,
      meta : {
      auth: false,
      },
      },
      {
      path: '/',
      redirect: '/dashboard',
      name: 'Home',
      component: DefaultContainer,
      meta : {
      auth: false,
      },
      children: [
      {
      path : 'credenciales',
      redirect : '/credenciales/proveedores',
      name : 'Credenciales',
      component : {
      render (c) { return c('router-view') }
      },
      children: [
      {
      path : 'proveedores',
      name : 'Proveedores',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'tester',
      lock : true,
      }
      ]
      },
      },
      {
      path : 'accesos',
      name : 'Accesos',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'admin',
      lock : true,
      }
      ]
      },
      },
      ]
      },
      ]
      })








      router.beforeEach((to,from, next)=>{
      const rolesToPath = (to.meta.roles)?to.meta.roles:null //roles authorized in next path
      const reqAuth = to.matched.some(record=> record.meta.auth)
      const currentUser = store.state.currentUser //current user With Roles
      const homeRoute = '/dashboard'
      const loginRoute = '/login'

      if (reqAuth && currentUser) {
      let auth = matchRoles(rolesToPath,currentUser.roles)
      if (auth) {
      next()
      } else {
      next(homeRoute)
      }
      }


      function matchRoles(x,y){ //this function resolve if user is authorized
      // to next path
      let flag = false
      x.forEach((x)=>{
      y.forEach((y)=>{
      if (x.userRol==y.name) {
      flag = true//if role user is on next path
      }
      })
      })

      if (flag) return true
      return false

      }

      })





      router.beforeEach((to,from, next)=>{
      const rolesToPath = (to.meta.roles)?to.meta.roles:null //roles authorized in next path
      const reqAuth = to.matched.some(record=> record.meta.auth)
      const currentUser = store.state.currentUser //current user With Roles
      const homeRoute = '/dashboard'
      const loginRoute = '/login'

      if (reqAuth && currentUser) {
      let auth = matchRoles(rolesToPath,currentUser.roles)
      if (auth) {
      next()
      } else {
      next(homeRoute)
      }
      }


      function matchRoles(x,y){ //this function resolve if user is authorized
      // to next path
      let flag = false
      x.forEach((x)=>{
      y.forEach((y)=>{
      if (x.userRol==y.name) {
      flag = true//if role user is on next path
      }
      })
      })

      if (flag) return true
      return false

      }

      })





      import Vue from 'vue'
      import VueRouter from 'vue-router'
      Vue.use(VueRouter)

      export default new VueRouter({
      mode: 'history', // https://router.vuejs.org/api/#mode
      linkActiveClass: 'open active',
      scrollBehavior: () => ({ y: 0 }),
      routes:[
      {
      path: '/login',
      name: 'Login',
      component: Login,
      meta : {
      auth: false,
      },
      },
      {
      path: '/register',
      name: 'Register',
      component: Register,
      meta : {
      auth: false,
      },
      },
      {
      path: '/',
      redirect: '/dashboard',
      name: 'Home',
      component: DefaultContainer,
      meta : {
      auth: false,
      },
      children: [
      {
      path : 'credenciales',
      redirect : '/credenciales/proveedores',
      name : 'Credenciales',
      component : {
      render (c) { return c('router-view') }
      },
      children: [
      {
      path : 'proveedores',
      name : 'Proveedores',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'tester',
      lock : true,
      }
      ]
      },
      },
      {
      path : 'accesos',
      name : 'Accesos',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'admin',
      lock : true,
      }
      ]
      },
      },
      ]
      },
      ]
      })





      import Vue from 'vue'
      import VueRouter from 'vue-router'
      Vue.use(VueRouter)

      export default new VueRouter({
      mode: 'history', // https://router.vuejs.org/api/#mode
      linkActiveClass: 'open active',
      scrollBehavior: () => ({ y: 0 }),
      routes:[
      {
      path: '/login',
      name: 'Login',
      component: Login,
      meta : {
      auth: false,
      },
      },
      {
      path: '/register',
      name: 'Register',
      component: Register,
      meta : {
      auth: false,
      },
      },
      {
      path: '/',
      redirect: '/dashboard',
      name: 'Home',
      component: DefaultContainer,
      meta : {
      auth: false,
      },
      children: [
      {
      path : 'credenciales',
      redirect : '/credenciales/proveedores',
      name : 'Credenciales',
      component : {
      render (c) { return c('router-view') }
      },
      children: [
      {
      path : 'proveedores',
      name : 'Proveedores',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'tester',
      lock : true,
      }
      ]
      },
      },
      {
      path : 'accesos',
      name : 'Accesos',
      component : TablaProveedores,
      meta : {
      auth: true,
      roles: [
      {
      userRol : 'admin',
      lock : true,
      }
      ]
      },
      },
      ]
      },
      ]
      })






      javascript vuejs2 vue-router






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 19:46









      Eleazar OrtegaEleazar Ortega

      941110




      941110
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326902%2fguard-function-on-vue-router-only-work-on-second-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326902%2fguard-function-on-vue-router-only-work-on-second-loop%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Bressuire

          Vorschmack

          Quarantine