diff --git a/README.md b/README.md index b7e7441..a1cb947 100644 --- a/README.md +++ b/README.md @@ -43,7 +43,8 @@ import "github.com/appleboy/gin-jwt" Please see [the example file](example/basic/server.go) and you can use `ExtractClaims` to fetch user data. -[embedmd]:# (example/basic/server.go go) +[embedmd]: # "example/basic/server.go go" + ```go package main @@ -137,6 +138,14 @@ func main() { return false }, + LoginResponse: func(c *gin.Context, code int, token string, expire time.Time, claims map[string]interface{}) { + c.JSON(http.StatusOK, gin.H{ + "code": http.StatusOK, + "token": token, + "expire": expire.Format(time.RFC3339), + identityKey: claims[identityKey], + }) + }, Unauthorized: func(c *gin.Context, code int, message string) { c.JSON(code, gin.H{ "code": code, diff --git a/auth_jwt.go b/auth_jwt.go index efb1d24..283e05e 100644 --- a/auth_jwt.go +++ b/auth_jwt.go @@ -63,7 +63,7 @@ type GinJWTMiddleware struct { Unauthorized func(*gin.Context, int, string) // User can define own LoginResponse func. - LoginResponse func(*gin.Context, int, string, time.Time) + LoginResponse func(*gin.Context, int, string, time.Time, map[string]interface{}) // User can define own LogoutResponse func. LogoutResponse func(*gin.Context, int) @@ -287,7 +287,7 @@ func (mw *GinJWTMiddleware) MiddlewareInit() error { } if mw.LoginResponse == nil { - mw.LoginResponse = func(c *gin.Context, code int, token string, expire time.Time) { + mw.LoginResponse = func(c *gin.Context, code int, token string, expire time.Time, claims map[string]interface{}) { c.JSON(http.StatusOK, gin.H{ "code": http.StatusOK, "token": token, @@ -465,7 +465,7 @@ func (mw *GinJWTMiddleware) LoginHandler(c *gin.Context) { ) } - mw.LoginResponse(c, http.StatusOK, tokenString, expire) + mw.LoginResponse(c, http.StatusOK, tokenString, expire, claims) } // LogoutHandler can be used by clients to remove the jwt cookie (if set)