matthew-andrews/isomorphic-fetch

set-cookie support in isomorphic-fetch in nodeJs application (question)

sundriver opened this issue · 5 comments

I am using isomorphic-fetch (2.2.1) in a NodeJS application. The response from server comes with set-cookie headers, but my subsequent isomorphic-fetch calls do not include the cookies. I am using credentials: "same-origin" (also tried credentials: "include" but not luck.

Does isomorphic fetch set-cookies when it is used in nodeJS (not a browser) ?

[also asked this in SO, with not response]

kopax commented

I am hitting the same issue. I am expecting to receive cookies but they are always unset. Any info ?

You would have to implement this yourself, something like……

fetch('https://mattandre.ws/endpoint-that-sets-set-cookie')
  .then(res => {
    const setCookie = res.headers.get('set-cookie');
    const cookie = setCookie.substr(0, setCookie.indexOf(';'))
    return fetch('https://mattandre.ws/endpoint-that-relies-on-cookies-set', {
      headers: { Cookie: cookie }
    });
  });
kopax commented

Thanks @matthew-andrews . This would obviously work, but could I know why it is not possible to mock document.cookie in nodejs?

The best thing would be to globally use some store for that instead of catching the cookie ourselves, any idea how it could be done ? I assume this will require some change in the library.

Hmm, I think that it might be a little dangerous to do by default. If you're using Cookies on the server side you are probably emulating logging in as a user. So by sharing that globally on the server side you have quite high risk of accidentally allowing users to access each other's data……

In case it helps anyone else, I was trying to use this in jest (jsdom) to test a browser app and cookies were not working. whatwg-fetch works fine in this regard, as an alternative.