This commit is contained in:
Gerasimos (Makis) Maropoulos 2023-12-25 23:55:31 +02:00
parent 26422ebaf4
commit fb8d8d0881
No known key found for this signature in database
GPG Key ID: B9839E9CD30B7B6B
2 changed files with 15 additions and 13 deletions

View File

@ -5946,23 +5946,25 @@ var (
CookieExpireUnlimited = time.Now().AddDate(24, 10, 10) CookieExpireUnlimited = time.Now().AddDate(24, 10, 10)
) )
// RemoveCookie deletes a cookie by its name and path = "/". // RemoveCookie deletes a cookie by its name. It reads the cookie's path and domain
// Tip: change the cookie's path to the current one by: RemoveCookie("name", iris.CookieCleanPath) // in order to delete the cookie cross-browser.
// Reports whether the cookie was removed or not.
// //
// Example: https://github.com/kataras/iris/tree/main/_examples/cookies/basic // Example: https://github.com/kataras/iris/tree/main/_examples/cookies/basic
func (ctx *Context) RemoveCookie(name string, options ...CookieOption) { func (ctx *Context) RemoveCookie(name string, options ...CookieOption) bool {
c := &http.Cookie{} // Get the cookie from the request
c.Name = name c, err := ctx.Request().Cookie(name)
c.Value = "" if err != nil {
c.Path = "/" // if user wants to change it, use of the CookieOption `CookiePath` is required if not `ctx.SetCookie`. return false
c.HttpOnly = true }
// RFC says 1 second, but let's do it 1 to make sure is working
c.Expires = CookieExpireDelete
c.MaxAge = -1
// Set the cookie expiration date to a past time
c.Expires = time.Unix(0, 0)
c.MaxAge = -1 // RFC says 1 second, but let's do it -1 to make sure is working.
// Send the cookie back to the client
ctx.applyCookieOptions(c, OpCookieDel, options) ctx.applyCookieOptions(c, OpCookieDel, options)
http.SetCookie(ctx.writer, c) http.SetCookie(ctx.writer, c)
return true
} }
// VisitAllCookies takes a visitor function which is called // VisitAllCookies takes a visitor function which is called

View File

@ -13,7 +13,7 @@ var (
Clock func() time.Time = time.Now Clock func() time.Time = time.Now
// ExpireDelete may be set on Cookie.Expire for expiring the given cookie. // ExpireDelete may be set on Cookie.Expire for expiring the given cookie.
ExpireDelete = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) ExpireDelete = time.Unix(0, 0) // time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
) )
// LifeTime controls the session expiration datetime. // LifeTime controls the session expiration datetime.