Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements CookieMaxAge value #245

Merged
merged 1 commit into from
May 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ type GinJWTMiddleware struct {
// Optionally return the token as a cookie
SendCookie bool

// Duration that a cookie is valid. Optional, by default equals to Timeout value.
CookieMaxAge time.Duration

// Allow insecure cookies for development over http
SecureCookie bool

Expand Down Expand Up @@ -335,6 +338,10 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error {
mw.Realm = "gin jwt"
}

if mw.CookieMaxAge == 0 {
mw.CookieMaxAge = mw.Timeout
}

if mw.CookieName == "" {
mw.CookieName = "jwt"
}
Expand Down Expand Up @@ -453,7 +460,8 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {

// set cookie
if mw.SendCookie {
maxage := int(expire.Unix() - time.Now().Unix())
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - time.Now().Unix())
c.SetCookie(
mw.CookieName,
tokenString,
Expand Down Expand Up @@ -536,7 +544,8 @@ func (mw *GinJWTMiddleware) RefreshToken(c *gin.Context) (string, time.Time, err

// set cookie
if mw.SendCookie {
maxage := int(expire.Unix() - time.Now().Unix())
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - time.Now().Unix())
c.SetCookie(
mw.CookieName,
tokenString,
Expand Down