In an attempt to construct a React application with a Go back-end, a cookie is set upon a request's response utilizing http.cookie. Despite this, the browser remains unable to store the cookie. This issue has been encountered in both Chrome and Firefox.
Upon closer inspection, it has been noticed that the 'credentials' attribute is absent from the Fetch API request expecting a response containing a cookie. By incorporating 'credentials: "include"' into the request, the browser is able to save the cookie acquired from the response.
fetch(`${url}/login`, { method: "POST", headers: { "Content-Type": "application/json", }, credentials: "include", // Added parameter body: JSON.stringify({ email: userDetails.email, password: userDetails.password, }), }).then((response) => { ...
This revelation highlights the significance of setting 'credentials' to "include" in requests expecting cookies in the response. The absence of this attribute prevents the browser from storing the cookie, resulting in the aforementioned issue.
The above is the detailed content of Why Doesn\'t My Browser Save Cookies from My Go Backend Unless I Specify `credentials: \'include\'`?. For more information, please follow other related articles on the PHP Chinese website!