反应前端的api调用之间没有持续的快速会话

问题描述 投票:0回答:2

在问这个问题之前,我确实看过其他类似的问题,但是到目前为止还没有任何帮助。

我有一个使用axios的反应前端,使用快速和快速会话对节点后端进行api调用。一旦我在前端输入登录信息,我就将它发送到我的后端,在那里我设置会话并返回我在localStorage中设置的前端的响应,用于客户端验证。

当我尝试访问受保护的api端点 - 从前端 - 登录后,会话没有我在其中设置的任何信息,因此,给我一个错误。但是,如果我尝试登录并从邮递员访问受保护的端点,它可以正常工作。

这是我的快速会话配置:

router.use(session({
 secret: 'notGoingToWork',
 resave: false,
 saveUninitialized: true
}))

这是api调用(登录后)我通过axios进行:

axios.get(`http://localhost:5000/users/personNotes/${JSON.parse(userData).email}`)
    .then(response => console.log(response);

我不知道问题是什么,并希望得到任何帮助。先感谢您!

node.js reactjs express axios express-session
2个回答
3
投票

尝试使用withCredentials

axios(`http://localhost:5000/users/personNotes/${JSON.parse(userData).email}`, {
  method: "get",
  withCredentials: true
})

要么

axios.defaults.withCredentials = true

1
投票

你可以使用axios尝试withCredentialstrue

使用credentials获取include也可以。

fetch(URL, 
 {
  credentials: 'include',
  method: 'POST',
  body: JSON.stringify(payload),
  headers: new Headers({
  'Content-Type': 'application/json'
 })
})
© www.soinside.com 2019 - 2024. All rights reserved.