Skip to content

Commit 88a7a68

Browse files
committed
docs(middleware): 完善JWT中间件函数的注释说明
1 parent 52543d4 commit 88a7a68

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

internal/pkg/middleware/AuthMiddleware.go

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ import (
2121
"github.com/gin-gonic/gin"
2222
)
2323

24-
// 初始化jwt中间件
24+
// InitAuth 初始化JWT中间件,配置认证、授权、登录、登出等处理函数
25+
// InitAuth initializes JWT middleware and configures authentication, authorization, login, logout handlers
2526
func InitAuth() (*jwt.GinJWTMiddleware, error) {
2627
authMiddleware, err := jwt.New(&jwt.GinJWTMiddleware{
2728
Realm: config.Conf.Jwt.Realm, // jwt标识
@@ -43,7 +44,8 @@ func InitAuth() (*jwt.GinJWTMiddleware, error) {
4344
return authMiddleware, err
4445
}
4546

46-
// 有效载荷处理
47+
// payloadFunc 处理JWT有效载荷,将用户数据转换为Claims格式
48+
// payloadFunc handles JWT payload and converts user data to Claims format
4749
func payloadFunc(data interface{}) jwt.MapClaims {
4850
if v, ok := data.(map[string]interface{}); ok {
4951
var user model.Admin
@@ -57,7 +59,8 @@ func payloadFunc(data interface{}) jwt.MapClaims {
5759
return jwt.MapClaims{}
5860
}
5961

60-
// 解析Claims
62+
// identityHandler 解析JWT Claims,提取用户身份信息
63+
// identityHandler parses JWT Claims and extracts user identity information
6164
func identityHandler(c *gin.Context) interface{} {
6265
claims := jwt.ExtractClaims(c)
6366
// 此处返回值类型map[string]interface{}与payloadFunc和authorizator的data类型必须一致, 否则会导致授权失败还不容易找到原因
@@ -67,7 +70,8 @@ func identityHandler(c *gin.Context) interface{} {
6770
}
6871
}
6972

70-
// 校验token的正确性, 处理登录逻辑
73+
// login 校验用户登录凭据,验证用户名和密码的正确性
74+
// login validates user login credentials and verifies username and password
7175
func login(c *gin.Context) (interface{}, error) {
7276
var req vo.RegisterAndLoginRequest
7377
// 请求json绑定
@@ -101,7 +105,8 @@ func login(c *gin.Context) (interface{}, error) {
101105
}, nil
102106
}
103107

104-
// 用户登录校验成功处理
108+
// authorizator 处理用户登录校验成功后的授权逻辑,将用户信息保存到上下文
109+
// authorizator handles authorization logic after successful login validation and saves user info to context
105110
func authorizator(data interface{}, c *gin.Context) bool {
106111
if v, ok := data.(map[string]interface{}); ok {
107112
userStr := v["user"].(string)
@@ -115,16 +120,18 @@ func authorizator(data interface{}, c *gin.Context) bool {
115120
return false
116121
}
117122

118-
// 用户登录校验失败处理
123+
// unauthorized 处理用户登录校验失败的情况,返回本地化的错误信息
124+
// unauthorized handles user login validation failure and returns localized error messages
119125
func unauthorized(c *gin.Context, code int, message string) {
120126
common.Log.Debugf("JWT认证失败, 错误码: %d, 错误信息: %s", code, message)
121127
// 使用本地化的JWT认证失败消息,并包含具体的错误信息
122128
localizedMsg := fmt.Sprintf("%s: %s", common.Msg(c, common.MsgJWTAuthFail), message)
123129
response.Unauthorized(c, localizedMsg)
124130
}
125131

126-
// 登录成功后的响应
127-
func loginResponse(c *gin.Context, code int, token string, expires time.Time) {
132+
// loginResponse 处理用户登录成功后的响应,返回JWT令牌和过期时间
133+
// loginResponse handles the response after successful user login and returns JWT token and expiration time
134+
func loginResponse(c *gin.Context, _code int, token string, expires time.Time) {
128135
msg := common.Msg(c, common.MsgLoginSuccess)
129136
response.Success(c,
130137
gin.H{
@@ -134,14 +141,16 @@ func loginResponse(c *gin.Context, code int, token string, expires time.Time) {
134141
msg)
135142
}
136143

137-
// 登出后的响应
138-
func logoutResponse(c *gin.Context, code int) {
144+
// logoutResponse 处理用户登出后的响应,返回本地化的成功消息
145+
// logoutResponse handles the response after user logout and returns a localized success message
146+
func logoutResponse(c *gin.Context, _code int) {
139147
msg := common.Msg(c, common.MsgLogoutSuccess)
140148
response.Success(c, nil, msg)
141149
}
142150

143-
// 刷新token后的响应
144-
func refreshResponse(c *gin.Context, code int, token string, expires time.Time) {
151+
// refreshResponse 处理刷新JWT令牌后的响应,返回新的令牌和过期时间
152+
// refreshResponse handles the response after JWT token refresh and returns new token and expiration time
153+
func refreshResponse(c *gin.Context, _code int, token string, expires time.Time) {
145154
msg := common.Msg(c, common.MsgRefreshTokenSuccess)
146155
response.Success(c,
147156
gin.H{

0 commit comments

Comments
 (0)