【问题标题】:react and passport missing token反应和护照丢失令牌
【发布时间】:2018-07-06 09:06:37
【问题描述】:

我正在构建一个使用 API 与另一台服务器通信的反应应用程序。 API 调用正在使用axios,但在后端的护照身份验证上一直失败。 使用 Postman 进行身份验证,它与反应应用程序一起工作正常, res.isAuthenticaded 的值为 false。 这是使用邮递员到达服务器的值

[Node] req session--> Session {
[Node]   cookie: 
[Node]    { path: '/',
[Node]      _expires: null,
[Node]      originalMaxAge: null,
[Node]      httpOnly: true },
[Node]   passport: { user: '5a6a04f017589a97237dc9df' },
[Node]   returnTo: '/user/account' }
[Node] req session id--> a9uTD17j9fnPUYHd4srsfmvSfW6G3JZQ
[Node] isAuthenticated--> true
[Node] GET /user/account 200 210.616 ms - 54

这就是 react 应用的价值

Node] req session--> Session {
[Node]   cookie: 
[Node]    { path: '/',
[Node]      _expires: null,
[Node]      originalMaxAge: null,
[Node]      httpOnly: true },
[Node]   returnTo: '/user/account' }
[Node] req session id--> 2cWySg06ZZuozdS4nklfnd-8H5iPYDsC
[Node] isAuthenticated--> false
[Node] GET /user/account 403 56.948 ms - 28

【问题讨论】:

  • 您是否在使用会话存储?如果没有,它可能会解决您的问题
  • 是的,我的请求确实具有Cookie 值,但似乎我发送服务器的方式不会处理它。附上我的 cookie 的打印件

标签: reactjs cookies react-redux passport.js axios


【解决方案1】:

显然,axios 包没有正确发回标头以便与 passport 一起使用,因为 passport 令牌保护正在寻找 X-CSRF-TOKEN,但 axios 发送 X-XSRF-令牌。 为了解决我的问题,我改用这个包来完成我的请求whatwg-fetch,就像一个魅力。 我希望这对以后遇到同样问题的人有所帮助。

【讨论】:

    猜你喜欢
    • 2019-11-17
    • 1970-01-01
    • 2019-05-15
    • 2020-01-14
    • 2020-11-23
    • 2019-01-29
    • 1970-01-01
    • 2019-08-13
    • 2019-09-13
    相关资源
    最近更新 更多