2

I am using the following guard const to prevent user from entering pages while they don't have a token saved on cookies. However if i try it enough time the user is able to enter the page but the components don't load, is is just a blank page

What else should i add to my router file to prevent this kind of thing from happening?

Here is my guard constant:

const guard = function(to, from, next) {
  const token = Cookies.get('token')
  if(typeof token === 'undefined' || token === null ){
    this.$store.dispatch('logout')
    window.location.href = "/";
  } else {
    next();
  }
};
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164

1 Answers1

1

Try to use next('/') instead of window.location.href = "/" :

import store from 'path/to/store/
const guard = function(to, from, next) {
  const token = Cookies.get('token')
  if(typeof token === 'undefined' || token === null ){
   store.dispatch('logout');
    next('/');
  } else {
    next();
  }
};
Boussadjra Brahim
  • 82,684
  • 19
  • 144
  • 164