小编mas*_*er6的帖子

LocalStorage 中的令牌存在,但没有放入我的 Axios 实例中

我的组件之一未加载,因为本地存储中的令牌未在我的 Axios 请求中发送。

\n

这是流程。我有一个Login组件,一旦用户经过身份验证,它就会在本地存储中设置令牌。这是该函数:

\n
   AuthService.authenticateEmployee(employee).then((r) => {\n      if (r != null && r.data.jwt != null) {\n        localStorage.setItem("token", r.data.jwt);\n        console.log(r.data.jwt);\n        console.log("nav to tickets now");\n        this.props.history.push("/tickets");\n      }\n    });\n  };\n
Run Code Online (Sandbox Code Playgroud)\n

然后它会带您到/tickets我的问题所在。尝试TicketsComponent\n验证令牌。如果无效,则应将用户重定向回登录页面。

\n
   AuthService.validateToken()\n      .then((res) => {\n        console.log("step 1");\n        if (res == undefined || res == null || !token) {\n          this.props.history.push("/login");\n        }\n      })\n      .then(() => {\n        console.log("step 2");\n\n        TicketService.getTickets().then((res) => {\n          if (this._isMounted) {\n            this.setState({ tickets: res.data });\n          }\n        });\n      });\n
Run Code Online (Sandbox Code Playgroud)\n

这是validateToken(): …

local-storage reactjs

2
推荐指数
1
解决办法
2745
查看次数

标签 统计

local-storage ×1

reactjs ×1