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

feat: response claims surpport for LoginResponse #225

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down