admin管理员组文章数量:1026989
So here's my problem. I have a dasboard page and i have programs page. The problem is on the programs page where i have SSR, because on dashboard page i call my saga on client-side and everything works like it should work.
Client side: The client sends the httpOnly cookie to my backend server and data is fetched from my backend for the use.
Server side: However for some reason when i call the same saga inside getServerSideProps of course {withCredentials: true} it doesn't send the token to my backend for some reason. Inside the req object i get from getServerSideProps inside req.headers.cookie i see the cookie, but it doesn't send it. So what's the solution to manually add it when calling it form getServerSideProps or?
The code:
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)
So here's my problem. I have a dasboard page and i have programs page. The problem is on the programs page where i have SSR, because on dashboard page i call my saga on client-side and everything works like it should work.
Client side: The client sends the httpOnly cookie to my backend server and data is fetched from my backend for the use.
Server side: However for some reason when i call the same saga inside getServerSideProps of course {withCredentials: true} it doesn't send the token to my backend for some reason. Inside the req object i get from getServerSideProps inside req.headers.cookie i see the cookie, but it doesn't send it. So what's the solution to manually add it when calling it form getServerSideProps or?
The code:
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)
Share
Improve this question
asked Aug 10, 2020 at 20:54
Blagoj PetrovBlagoj Petrov
2951 gold badge6 silver badges13 bronze badges
1 Answer
Reset to default 5I believe the cookie is stored on the client side (browser).
May be this can work, as long as you can make the cookie reach the saga.
// The axios inside the saga:
const res = yield axios.get(url, {
headers: {
Cookie: "cookie1=value; cookie2=value; cookie3=value;"
});
Another option, if you are using an access token is sending it like using an authorization header.
const res = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` },
});
So here's my problem. I have a dasboard page and i have programs page. The problem is on the programs page where i have SSR, because on dashboard page i call my saga on client-side and everything works like it should work.
Client side: The client sends the httpOnly cookie to my backend server and data is fetched from my backend for the use.
Server side: However for some reason when i call the same saga inside getServerSideProps of course {withCredentials: true} it doesn't send the token to my backend for some reason. Inside the req object i get from getServerSideProps inside req.headers.cookie i see the cookie, but it doesn't send it. So what's the solution to manually add it when calling it form getServerSideProps or?
The code:
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)
So here's my problem. I have a dasboard page and i have programs page. The problem is on the programs page where i have SSR, because on dashboard page i call my saga on client-side and everything works like it should work.
Client side: The client sends the httpOnly cookie to my backend server and data is fetched from my backend for the use.
Server side: However for some reason when i call the same saga inside getServerSideProps of course {withCredentials: true} it doesn't send the token to my backend for some reason. Inside the req object i get from getServerSideProps inside req.headers.cookie i see the cookie, but it doesn't send it. So what's the solution to manually add it when calling it form getServerSideProps or?
The code:
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)
Share
Improve this question
asked Aug 10, 2020 at 20:54
Blagoj PetrovBlagoj Petrov
2951 gold badge6 silver badges13 bronze badges
1 Answer
Reset to default 5I believe the cookie is stored on the client side (browser).
May be this can work, as long as you can make the cookie reach the saga.
// The axios inside the saga:
const res = yield axios.get(url, {
headers: {
Cookie: "cookie1=value; cookie2=value; cookie3=value;"
});
Another option, if you are using an access token is sending it like using an authorization header.
const res = await axios.get(url, {
headers: { Authorization: `Bearer ${token}` },
});
本文标签:
版权声明:本文标题:javascript - HttpOnly cookie is not sent from next js getServerSideProps using axios (withCredentials: true) - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745661686a2161928.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论