"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why Isn\'t My Browser Saving Cookies Set by My React/Go App?

Why Isn\'t My Browser Saving Cookies Set by My React/Go App?

Posted on 2025-03-04
Browse:198

Why Isn\'t My Browser Saving Cookies Set by My React/Go App?

Cookie Not Retained by Browser

You've encountered an issue where your React app integrated with a Go server sets a cookie upon login but the browser fails to save it. Let's delve into the potential causes:

In your HTTP response, you have correctly set the cookie details using http.Cookie. However, for the browser to retain it, the 'credentials' flag must be set to 'include' when making the fetch request that expects the cookie in the response.

The following steps should resolve your issue:

  1. Update Request: When initiating the fetch request that expects the cookie in the response, ensure that you specify credentials: 'include'. This enables the browser to include any relevant cookies in the request.
  2. Modify Fetch Call: Here's an example of how to update your fetch call:

    fetch(`${url}/login`, {
                 method: "POST",
                 headers: {
                     "Content-Type": "application/json",
                 },
                 credentials: "include", // This is the key modification
                 body: JSON.stringify({
                     email: userDetails.email,
                     password: userDetails.password,
                 }),
             }).then((response) => { ...

By incorporating these changes, your browser should now correctly retain the cookie set by your Go server.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3