How to handle login JWT token in Vue JS?

Viewed 49

I have a token in my localstorage. if I delete the token in localstorage, then I'm not logging out. Can you suggest me example how to do it.

created() {
  if (this.vueToken) {
  let headers = {
    Authorization: "Bearer " + localStorage.getItem("vueToken"),
  };

  axios
    .get("checkLogin", {
      headers: headers,
    })
    .then((response) => response);
}else{
    this.$router.push('/login')
}
  },
1 Answers

You have to set up a condition in your secure routes, for example, if token is null in your localstorage then you will push it to local storage. Here is an example code.

mounted() {
this.token = localStorage.getItem("ivToken");    
if(this.token===null){     
  this.$router.push("/signin")
}

},`

Related