This commit is contained in:
oneao committed 2026-02-26 17:31:08 +08:00
1 parent 4ce951dd56
commit 8a3dea8545
44 files changed
+1599 -30

No files matched your search

@@ -0,0 +1,47 @@
package middleware
import (
"allapp/utils/jwtx"
"allapp/utils/responsex"
"strings"
"github.com/gofiber/fiber/v3"
)
const CtxUserIDKey = "user_id"
// Auth 登录校验中间件
func Auth() fiber.Handler {
return func(c fiber.Ctx) error {
token := strings.TrimSpace(c.Get("Token"))
if token == "" {
return responsex.Unauthorized(c, "未登录")
}
verifyToken, ok := jwtx.VerifyToken(c.Context(), token)
if !ok {
return responsex.Unauthorized(c, "登录已过期")
}
c.Locals(CtxUserIDKey, verifyToken.UserID)
return c.Next()
}
}
func GetUserID(c fiber.Ctx) (int64, error) {
v := c.Locals(CtxUserIDKey)
if v == nil {
return 0, responsex.Unauthorized(c, "登录已过期")
}
userID, ok := v.(int64)
if !ok {
return 0, responsex.Unauthorized(c, "登录已过期")
}
return userID, nil
}