Home > Article > Backend Development > 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!
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
dev tools ->
application ->
storage ->
cookiesand my cookie does not exist)
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)
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}) }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> );Any clues..? Solution (Thanks to the
#Reactiflux channel on
Discord)
Service-Terminal:
AllowHeaders headers -> Add
"Access-Control-Allow-Headers", "Authorization"
AllowOriginFunc -> means disallow
* instead of specific domain
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!