From 571628710b314d5287a7cacda4af2845584079e3 Mon Sep 17 00:00:00 2001 From: EUForest Date: Tue, 14 Oct 2025 15:08:59 +0800 Subject: [PATCH] update: Device Middleware --- internal/middleware/deviceMiddleware.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/internal/middleware/deviceMiddleware.go b/internal/middleware/deviceMiddleware.go index 58f7b89..b66ccb0 100644 --- a/internal/middleware/deviceMiddleware.go +++ b/internal/middleware/deviceMiddleware.go @@ -40,20 +40,18 @@ func DeviceMiddleware(srvCtx *svc.ServiceContext) func(c *gin.Context) { return } - var loginType string - if c.Value(constant.CtxKeyUser) == nil { - loginType = c.GetHeader("Login-Type") - } else { - loginType = c.GetString(string(constant.LoginType)) + ctx := c.Request.Context() + if ctx.Value(constant.CtxKeyUser) == nil && c.GetHeader("Login-Type") != "" { + ctx = context.WithValue(ctx, constant.LoginType, c.GetHeader("Login-Type")) + c.Request = c.Request.WithContext(ctx) } - if loginType != "device" { + loginType, ok := ctx.Value(constant.LoginType).(string) + if !ok || loginType != "device" { c.Next() return } - c.Request = c.Request.WithContext(context.WithValue(c.Request.Context(), constant.LoginType, loginType)) - rw := NewResponseWriter(c, srvCtx) if !rw.Decrypt() { result.HttpResult(c, nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidCiphertext), "Invalid ciphertext"))