[Vuejs]-How to setup vuex and vue-router to redirect when a store value is not set?

2👍

I fixed the issue by returning a promise from vuex action and then run the validations

router.beforeEach((to, from, next) => {
  store.dispatch('auth/authenticate').then(response => {
    next()
  }).catch(error => {
    if (!error.message.includes('Could not find stored JWT')) {
      console.log('Authentication error', error)
    }
    (to.meta.requiresAuth) ? next('/inicio-sesion') : next()
  })
})

Leave a comment