Home >Backend Development >Golang >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?

Linda Hamilton
Linda HamiltonOriginal
2024-11-30 19:20:17472browse

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.

The above is the detailed content of Why Isn\'t My Browser Saving Cookies Set by My React/Go App?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn