gin icon indicating copy to clipboard operation
gin copied to clipboard

`c.SetCookie` function is missing expire field

Open jsjain opened this issue 1 year ago • 0 comments

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

jsjain avatar Aug 04 '22 05:08 jsjain

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

KKRaver avatar Aug 10 '22 07:08 KKRaver

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

jsjain avatar Aug 18 '22 07:08 jsjain