-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: demo how to get current user(apiKey)
- Loading branch information
1 parent
d5846fb
commit da1007a
Showing
3 changed files
with
43 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package utils | ||
|
||
import ( | ||
jwt "github.com/appleboy/gin-jwt/v2" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// CurrentUser is a util tool for getting current user(ApiKey) from each rpc request | ||
func CurrentUser(ctx *gin.Context) (exists bool, user string) { | ||
|
||
defer func() { | ||
if r := recover(); r != nil { | ||
exists = false | ||
} | ||
}() | ||
|
||
mapping := ctx.MustGet("JWT_PAYLOAD").(jwt.MapClaims) | ||
|
||
user = mapping["jti"].(string) | ||
|
||
exists = true | ||
|
||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters