Mr Day2Day
Mr Day2Day

Reputation: 93

React App, Express Server - Set cookie not being stored in browser

The long short is...

My express backend sets a cookie and sends it back to the client, everything works great when I use Postman and my React Native app. The issue is the web version which is done via ReactJs makes the same request but cookie is stored in the browser.

So I know cookies are working but not for the web version which is strange when I created a test endpoint http://localhost:3000/server and call it straight from the browser the cookie is stored.

So this is my code doing a simple fetch to the server which then sends back a cookie:

  const fetchData = async () => {
    try {
      const res = await fetch(`http://192.168.0.11:3000/server/`, {
        credentials: "include",
      });
      if (res.ok) {
        const resData = await res.json();
        console.log({ resData });
      }
    } catch (err) {
      console.log({ err });
    }
  };

The request came back successful but no cookie is stored

enter image description here

Access the same endpoint from the browser directly results in this:

enter image description here

enter image description here

An extract from the response header while being sent shows that the cookie was sent in the response just not stored in the frontend

enter image description here

Upvotes: 3

Views: 2763

Answers (1)

Mr Day2Day
Mr Day2Day

Reputation: 93

Was a pretty simple fix

I was using http://localhost:3001 for the react app I just simply used the ip address instead http://192.168.0.11:3001

Upvotes: 1

Related Questions