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