Description
Currently c.SetCookie
does not accept cookie Expire
field which is present in http.Cookie struct and might be important if you want to maintain short lived sessions via cookie method.
I am aware that we can set cookie via-
http.SetCookie(c.Writer, &http.Cookie{
Name: "session",
Value: "some-session-value",
Domain: "localhost",
Expires: "Expiry time", <- this is missing
Secure: false,
HttpOnly: false,
})
But since we are already exposing a helper function to set cookie, i suppose extending it to support expire would be helpful for others.
How to reproduce
c.SetCookie("session", "some-session-value", 0, "/", "localhost", false, false)
Expectations
c.SetCookie("session", "some-session-value", 0, "/", "localhost", false, false, timeToExpire) <- adding additional param for time to expire
I can raise the PR for the same sine this is fairly small change and would love to contribute to this project.
Environment
- go version: 1.18.5
- gin version (or commit ref): v1.8.1
- operating system: Platform Agnostic
Comment From: tnothy
I believe what you need is available via maxAge param in the SetCookie method.
func (c *Context) SetCookie(name, value string, maxAge int, path, domain string, secure, httpOnly bool)
source
Comment From: jsjain
got it, after some further reading got to know that expires is now deprated and max-age
takes priority in case both are set.
SO answer