首頁  >  問答  >  主體

如何在Vue JS中處理登入JWT令牌?

我的本機儲存中有一個令牌。如果我刪除 localstorage 中的令牌,那麼我就不會登出。 你能給我舉個例子來說明如何做到這一點嗎?

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 天前326

全部回覆(1)我來回復

  • P粉511896716

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

    您必須在安全路由中設定一個條件,例如,如果本機儲存中的令牌為空,則將其推送到本機儲存。這是範例程式碼。

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

    },`

    回覆
    0
  • 取消回覆