Skip to content

Commit

Permalink
chore: support CookieSameSite
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Jul 15, 2020
1 parent 653dcba commit fd0cbae
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ type GinJWTMiddleware struct {

// CookieName allow cookie name change for development
CookieName string

// CookieSameSite allow use http.SameSite cookie param
CookieSameSite http.SameSite
}

var (
Expand Down Expand Up @@ -462,6 +465,11 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - mw.TimeFunc().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
tokenString,
Expand All @@ -480,6 +488,10 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) {
func (mw *GinJWTMiddleware) LogoutHandler(c *gin.Context) {
// delete auth cookie
if mw.SendCookie {
if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
"",
Expand Down Expand Up @@ -546,6 +558,11 @@ func (mw *GinJWTMiddleware) RefreshToken(c *gin.Context) (string, time.Time, err
if mw.SendCookie {
expireCookie := mw.TimeFunc().Add(mw.CookieMaxAge)
maxage := int(expireCookie.Unix() - time.Now().Unix())

if mw.CookieSameSite != 0 {
c.SetSameSite(mw.CookieSameSite)
}

c.SetCookie(
mw.CookieName,
tokenString,
Expand Down

0 comments on commit fd0cbae

Please sign in to comment.