gpt4 book ai didi

javascript - HttpOnly cookie 不会从下一个 js getServerSideProps 使用 axios 发送(withCredentials : true)

转载 作者:行者123 更新时间:2023-12-05 00:36:43 34 4
gpt4 key购买 nike

所以这是我的问题。我有一个仪表板页面,我有程序页面。 问题出在我拥有 SSR 的程序页面上,因为在仪表板页面上,我在客户端调用了我的传奇,并且一切正常。
客户端:客户端将 httpOnly cookie 发送到我的后端服务器,并从我的后端获取数据以供使用。
服务器端 :但是由于某种原因,当我在 getServerSideProps 中调用相同的传奇时,当然 {withCredentials: true} 它不会出于某种原因将 token 发送到我的后端。在我从 req.headers.cookie 内部的 getServerSideProps 获得的 req 对象中,我看到了 cookie,但它没有发送它。那么当从 getServerSideProps 调用它时手动添加它的解决方案是什么?
编码:

export const getServerSideProps = wrapper.getServerSideProps(
async ({ store, req }) => {
store.dispatch(fetchprogramsRequest(req.url));
const cookies = cookie.parse(req.headers.cookie);
console.log('COOKIES', cookies); // HERE you can see the cookies

// end the saga
store.dispatch(END);
await store.sagaTask.toPromise();
}
);


The axios inside the saga:
const res = yield axios.get(url, { withCredentials: true });
This is called in both cases (client-side: works, server-side: doesn't)

最佳答案

我相信 cookie 存储在客户端(浏览器)上。
可能这可以工作,只要你能让 cookies 到达传奇。

// The axios inside the saga:
const res = yield axios.get(url, {
headers: {
Cookie: "cookie1=value; cookie2=value; cookie3=value;"
});
另一种选择,如果您使用访问 token ,则像使用授权 header 一样发送它。
 const res = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` },
});

关于javascript - HttpOnly cookie 不会从下一个 js getServerSideProps 使用 axios 发送(withCredentials : true),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63347884/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com