Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
sundriver opened this issue Nov 8, 2017 · 5 comments
Open

Comments

@sundriver
Copy link

sundriver commented Nov 8, 2017

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
Copy link

kopax commented Nov 24, 2017

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

@matthew-andrews
Copy link
Owner

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
Copy link

kopax commented Nov 30, 2017

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
Copy link
Owner

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
Copy link

Dreamsorcerer commented Dec 21, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants