module
Version:
v1.1.0
Opens a new window with list of versions in this module.
Published: Apr 14, 2025
License: Apache-2.0
Opens a new window with license information.
README
¶
This repo is based on the great work of https://github.com/goflyfox/gtoken. It offers a simple way for golang beginners to use GoFrame.
Features
- Use jwt.
- Support cache/redis/file to store tokens.
- Support refresh jwt automatically.
- Support GoFrame v2.*. Currently, it's 2.9.0.
- Work as a group routing middleware(best practise of GoFrame).
- NanoID is used to reduce token bytes.
Documents
- Cache mode
- Cache: 0
- Redis: 1
- File: 2
- Choose to refresh token automatically by setting AutoRefreshToken true
- Handle public paths(non-auth parts)
- Public paths can be defined simply as []string{"/validation-code", "/activation"}
- Restful formats like "POST:/activation" are also supported.
- It's OK to add "/login" in PublicPaths or use a seperated group to bind the controller contains "/login".
- Initialize
- UseMiddleware will automatically apply Init()
- Init() is exposed basically for testing purpose
- Add extra info
- The default DoAfterAuth func can set the given g.Map into context.
- If a self-defined DoAfterAuth is given, use the following code.
for k, v := range data {
r.SetCtxVar(k, v)
}
- Response format
- gtoken is designed to avoid writing response directly.
- A custom response can be applied by defining a new DoAfterAuth.
- Token length
- Refer to gtoken.GToken to get more parameter details
Usage
gToken := >oken.GToken{
PublicPaths: []string{"POST:/login", "/logout"},
ExpireIn: 1 * time.Hour,
}
s.Group("/", func(group *ghttp.RouterGroup) {
err := gToken.UseMiddleware(ctx, group)
if err != nil {
panic(err)
}
group.GET("/user", func(r *ghttp.Request) {
r.Response.WriteJson(gtoken.DefaultResponse{Msg: "get user success"})
})
})
Directories
¶
Click to show internal directories.
Click to hide internal directories.