这是我的代码,我从 go-gin 框架文档中复制它,我的问题是当
createCookie
函数命中时它返回“NotSet”,我必须运行相同的 API 回调来获取 cookie。
// createCookie accepts c as gin context and create cookies
func createCookie(c *gin.Context) (string, error) {
cookie, err := c.Cookie("device")
if err != nil {
cookie = "NotSet"
c.SetCookie("device", "test", 3600, "/", os.Getenv("AUTH_DOMAIN"), true, true)
}
return cookie, nil
}
// createCookie accepts c as gin context and create cookies
func createCookie(c *gin.Context) (string, error) {
cookie, err := c.Cookie("device")
if err != nil {
// Replae it with your initial value
cookie = "NotSet"
c.SetCookie("device", cookie, 3600, "/", os.Getenv("AUTH_DOMAIN"), true, true)
}
return cookie, nil
}
http.SetCookie(context.Writer, &http.Cookie{
Name: "token",
Value: url.QueryEscape(newToken),
MaxAge: maxAge,
Path: "/",
Domain: "",
SameSite: http.SameSiteNoneMode,
Secure: true,
HttpOnly: true,
})