41 lines
769 B
Go
41 lines
769 B
Go
package middleware
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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, "登录已过期")
|
|
//}
|
|
//
|
|
//c.Locals(CtxUserIDKey, verifyToken.Data["user_id"])
|
|
|
|
ctx := context.WithValue(c.Context(), CtxUserIDKey, int64(779895419121733))
|
|
c.SetContext(ctx)
|
|
|
|
return c.Next()
|
|
}
|
|
}
|
|
|
|
func GetUserID(ctx context.Context) (int64, bool) {
|
|
v := ctx.Value(CtxUserIDKey)
|
|
|
|
id, ok := v.(int64)
|
|
|
|
return id, ok
|
|
}
|