isomorphic-fetch icon indicating copy to clipboard operation
isomorphic-fetch copied to clipboard

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

Open sundriver opened this issue 8 years ago • 5 comments
trafficstars

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]

sundriver avatar Nov 08 '17 21:11 sundriver

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

kopax avatar Nov 24 '17 04:11 kopax

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 }
    });
  });

matthew-andrews avatar Nov 24 '17 05:11 matthew-andrews

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.

kopax avatar Nov 30 '17 17:11 kopax

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

matthew-andrews avatar Nov 30 '17 17:11 matthew-andrews

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.

Dreamsorcerer avatar Dec 21 '23 17:12 Dreamsorcerer