Home  >  Q&A  >  body text

How to handle login JWT token in Vue JS?

There is a token in my localstorage. If I delete the token in localstorage then I don't log out. Can you give me an example of how to do this?

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

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

P粉587780103P粉587780103202 days ago328

reply all(1)I'll reply

  • P粉511896716

    P粉5118967162024-03-31 10:49:32

    You must set a condition in the security route, for example, if the token in local storage is empty, push it to local storage. Here is sample code.

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

    },`

    reply
    0
  • Cancelreply