javascript - HttpOnly cookie is not sent from next js getServerSideProps using axios (withCredentials: true) - Stack Overflow

So here's my problem. I have a dasboard page and i have programs page. The problem is on the progr

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
Add a ment  | 

1 Answer 1

Reset to default 5

I 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}` },
    });

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745661687a4638874.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信