-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Closed
Description
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()
})
}
SanketKarve, adamreisnz, hosein97, kirillbaryba, bchellingworth and 2 more
Metadata
Metadata
Assignees
Labels
No labels