Skip to content

Get the parent meta #704

@gil0mendes

Description

@gil0mendes

On the previous version of vue-router I was able to apply the session validation directly in the parent, but this not work on the new version?! I have to repeat the meta every route?

The code below shows a simplified version of the code who I'm using:

export const routes = [
  {
    path: '/platform',
    meta: { auth: true },
    component: require('./components/Base.vue'),

    children: [
      // users
      {
        path: '/users',
        name: 'users',
        component: require('./components/Users.vue')
      }
    ]
  }
]

export const startSecurity = router => {
  // get login token from the localStorage
  const token = localStorage.getItem('token')

  // before each transition we check if the route need authentication
  router.beforeEach((route, redirect, next) => {
    // check if the user needs to be authenticated. If the yes, redirect to the
    // login page if the token is null
    if (route.meta.auth && token === null) {
      return redirect({ name: 'login' })
    }

    // check if a logged user should see this page
    if (route.meta.guest && token !== null) {
      return redirect({ name: 'dashboard' })
    }

    // authorize transition
    next()
  })
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions