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.
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