第 1 阶段:用户与认证, mock登陆ok

This commit is contained in:
yml
2026-05-22 15:21:36 +08:00
parent 8e7f1f17f0
commit 9a79603e10
21 changed files with 780 additions and 4 deletions
+38
View File
@@ -0,0 +1,38 @@
package middleware
import (
"strings"
"hfb_sys/backend/internal/modules/auth"
"hfb_sys/backend/pkg/response"
"github.com/gin-gonic/gin"
)
const (
ContextUserID = "user_id"
ContextPhone = "phone"
)
func Auth(jwtManager *auth.JWTManager) gin.HandlerFunc {
return func(c *gin.Context) {
header := c.GetHeader("Authorization")
tokenText := strings.TrimSpace(strings.TrimPrefix(header, "Bearer "))
if tokenText == "" || tokenText == header {
response.Unauthorized(c, "缺少访问令牌")
c.Abort()
return
}
claims, err := jwtManager.Parse(tokenText, "access")
if err != nil {
response.Unauthorized(c, "访问令牌无效或已过期")
c.Abort()
return
}
c.Set(ContextUserID, claims.UserID)
c.Set(ContextPhone, claims.Phone)
c.Next()
}
}