20260507213409
This commit is contained in:
1 parent
3070eac71b
commit
4c7f516a03
204 files changed
+3623
No files matched your search
@@ -0,0 +1,44 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"allapp-go/internal/httpx"
|
||||
"allapp-go/pkg/jwtx"
|
||||
"context"
|
||||
"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 httpx.Unauthorized(c, "未登录")
|
||||
}
|
||||
|
||||
verifyToken, ok := jwtx.VerifyToken(c.Context(), token)
|
||||
if !ok {
|
||||
return httpx.Unauthorized(c, "登录已过期")
|
||||
}
|
||||
|
||||
ctx := context.WithValue(c.Context(), CtxUserIDKey, verifyToken.Data.UserID)
|
||||
|
||||
//ctx := context.WithValue(c.Context(), CtxUserIDKey, int64(796541901389893))
|
||||
|
||||
c.SetContext(ctx)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserID(ctx context.Context) (int64, bool) {
|
||||
v := ctx.Value(CtxUserIDKey)
|
||||
|
||||
id, ok := v.(int64)
|
||||
|
||||
return id, ok
|
||||
}
|
||||
Reference in new issue
Block a user