The browser did not save the cookie

王林
Release: 2024-02-09 10:30:09
forward
559 people have browsed it

The browser did not save the cookie

php Xiaobian Youzi is here to introduce a common problem - the browser does not save cookies. When we use browsers to access web pages on a daily basis, cookies are a common mechanism that can help websites remember the user's login status, personalized settings and other information. However, sometimes we encounter problems such as the browser not saving cookies, resulting in the loss of login status and the inability to browse the web normally. So why does this happen? Let’s explore it together!

Question content

Tried 15648070,15648070 unfortunately without success:)

Hello, it is my first time to use gin to build an api. I encountered some problems when setting cookies on the browser

What I mean is, when looking at the request on the dev tools, I see the set-cookie header with the correct value, also ## in that request #cookie I also saw cookie under the tab

The main problem is that it doesn't save on my browser (

dev tools -> application -> storage -> cookiesand my cookie does not exist)

rear end:

router.use(cors.new(cors.config{
        allowmethods:     []string{"get", "post", "put", "patch", "delete", "head", "options"},
        allowheaders:     []string{"origin", "content-length", "content-type"},
        maxage:           12 * time.hour,
        allowallorigins:  true,
        allowcredentials: true,
    }))

    router.post("/users/login", server.loginuser)
Copy after login
func (server *server) loginuser(ctx *gin.context) {
        ...
    ctx.setcookie("access_token", accesstoken, 3600, "/", "localhost", false, true)
    ctx.setcookie("refresh_token", refreshtoken, 3600, "/", "localhost", false, true)

    ctx.json(http.statusok, gin.h{"ok": true, "payload": rsps})

}
Copy after login

front end:

const login = async () => {
    const res = await fetch("http://localhost:3000/users/login", {
      method: "POST",
      body: JSON.stringify({ username, password }),
    });
    const data = await res.json();
    console.log(data);
  };
  const handleFormSubmit = (e) => {
    e.preventDefault();
    login();
  };

  return (
    <div>
      <h1>Login Page</h1>
      <form onSubmit={handleFormSubmit}>
         ...
        <button type="submit">Login</button>
      </form>
    </div>
  );
Copy after login

Any clues..?

Solution

(Thanks to the

#Reactiflux channel on Discord)

I missed two things..

Service-Terminal:

  • AllowHeaders headers -> Add "Access-Control-Allow-Headers", "Authorization"

  • Add

    AllowOriginFunc -> means disallow * instead of specific domain

front end:

    Add
  • withCredentials: true to my axios configuration

The above is the detailed content of The browser did not save the cookie. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!