matthew-andrews / isomorphic-fetch

Isomorphic WHATWG Fetch API, for Node & Browserify
MIT License
6.95k stars 289 forks source link

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

Open sundriver opened 6 years ago

sundriver commented 6 years ago

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 6 years ago

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

matthew-andrews commented 6 years ago

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 6 years ago

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.

matthew-andrews commented 6 years ago

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……

Dreamsorcerer commented 8 months ago

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.