-1

Need to do something like middleware, need to check if the user has a token, then allow the transition

router.beforeEach((to, from, next) => {
    const accessNeed = ['Dashboard',]
    if (localStorage.getItem("token")){
        if (!accessNeed.includes(to.name)) {
            next({ name: 'Home' })
        }else{
            next()
        }
    }else{
        next()
    }
})

enter image description here

Paolo
  • 3,530
  • 7
  • 21

1 Answers1

2

You are either using Nuxt, or just the Vue SSR package. So you have to make sure, the code gets executed only on client:

router.beforeEach((to, from, next) => {
  if (!process.client) {
    next()
    return
  }
    
  const accessNeed = ['Dashboard']

  if (window && window.localStorage.getItem("token")){
    if (!accessNeed.includes(to.name)) {
      next({ name: 'Home' })
    } else{
      next()
    }
  }

  next()
})
tho-masn
  • 1,126
  • 1
  • 7
  • 13