u
This commit is contained in:
1 parent
e893ef2e48
commit
b6415c0d65
25 files changed
+1495
-112
No files matched your search
@@ -1,9 +1,7 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"allapp-go/internal/httpx"
|
||||
"allapp-go/pkg/jwtx"
|
||||
"strings"
|
||||
"context"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
)
|
||||
@@ -13,35 +11,30 @@ 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, "未登录")
|
||||
}
|
||||
//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, "登录已过期")
|
||||
//}
|
||||
//
|
||||
//c.Locals(CtxUserIDKey, verifyToken.Data["user_id"])
|
||||
|
||||
verifyToken, ok := jwtx.VerifyToken(c.Context(), token)
|
||||
if !ok {
|
||||
return httpx.Unauthorized(c, "登录已过期")
|
||||
}
|
||||
|
||||
c.Locals(CtxUserIDKey, verifyToken.Data["user_id"])
|
||||
ctx := context.WithValue(c.Context(), CtxUserIDKey, int64(779895419121733))
|
||||
c.SetContext(ctx)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserID(c fiber.Ctx) (int64, error) {
|
||||
v := c.Locals(CtxUserIDKey)
|
||||
func GetUserID(ctx context.Context) (int64, bool) {
|
||||
v := ctx.Value(CtxUserIDKey)
|
||||
|
||||
if v == nil {
|
||||
return 0, httpx.Unauthorized(c, "登录已过期")
|
||||
}
|
||||
id, ok := v.(int64)
|
||||
|
||||
userID, ok := v.(int64)
|
||||
|
||||
if !ok {
|
||||
return 0, httpx.Unauthorized(c, "登录已过期")
|
||||
}
|
||||
|
||||
return userID, nil
|
||||
return id, ok
|
||||
}
|
||||
Reference in new issue
Block a user