Search code examples
httpgocookiesgo-gin

How to check if a cookie is expired


How do I check if a cookie I get through a GET request is expired or not? I tried:

cookie, err := c.Request.Cookie("session")
if err == nil && time.Now().Before(cookie.Expires) {
    log.Printf("COOKIE IS STILL GOOD. YUM!\n")
    return
}

But when I debug I see the expiration date is incorrect:

2019/05/15 01:23:46 0001-01-01 00:00:00 +0000 UTC

The date is today, rather than 2051 like I set it to be, and nothing else makes sense. What is happening?


Solution

  • Because clients send names and values only in a Cookie header, the Name and Value fields are the only fields set in this context. The Expires field has the time.Time zero value as the log output indicates.

    The application must do something outside of the cookie protocol to determine if the cookie is still valid.