From de6661cc16795c78477f33bd3c7592abc9eec435 Mon Sep 17 00:00:00 2001 From: EUForest Date: Thu, 6 Nov 2025 15:32:29 +0800 Subject: [PATCH] Update: Successful authentication records the identifier. --- internal/logic/auth/resetPasswordLogic.go | 7 ++++--- internal/logic/auth/telephoneLoginLogic.go | 7 ++++--- internal/logic/auth/telephoneResetPasswordLogic.go | 7 ++++--- internal/logic/auth/telephoneUserRegisterLogic.go | 7 ++++--- internal/logic/auth/userLoginLogic.go | 7 ++++--- internal/logic/auth/userRegisterLogic.go | 7 ++++--- internal/middleware/deviceMiddleware.go | 4 ++-- pkg/constant/context.go | 3 ++- 8 files changed, 28 insertions(+), 21 deletions(-) diff --git a/internal/logic/auth/resetPasswordLogic.go b/internal/logic/auth/resetPasswordLogic.go index aef245a..22db2c9 100644 --- a/internal/logic/auth/resetPasswordLogic.go +++ b/internal/logic/auth/resetPasswordLogic.go @@ -121,8 +121,8 @@ func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordRequest) (res // Don't fail register if device binding fails, just log the error } } - if l.ctx.Value(constant.LoginType) != nil { - req.LoginType = l.ctx.Value(constant.LoginType).(string) + if l.ctx.Value(constant.CtxLoginType) != nil { + req.LoginType = l.ctx.Value(constant.CtxLoginType).(string) } // Generate session id sessionId := uuidx.NewUUID().String() @@ -133,7 +133,8 @@ func (l *ResetPasswordLogic) ResetPassword(req *types.ResetPasswordRequest) (res l.svcCtx.Config.JwtAuth.AccessExpire, jwt.WithOption("UserId", userInfo.Id), jwt.WithOption("SessionId", sessionId), - jwt.WithOption("LoginType", req.LoginType), + jwt.WithOption("identifier", req.Identifier), + jwt.WithOption("CtxLoginType", req.LoginType), ) if err != nil { l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error())) diff --git a/internal/logic/auth/telephoneLoginLogic.go b/internal/logic/auth/telephoneLoginLogic.go index 8a54ff5..08c188a 100644 --- a/internal/logic/auth/telephoneLoginLogic.go +++ b/internal/logic/auth/telephoneLoginLogic.go @@ -137,8 +137,8 @@ func (l *TelephoneLoginLogic) TelephoneLogin(req *types.TelephoneLoginRequest, r } } - if l.ctx.Value(constant.LoginType) != nil { - req.LoginType = l.ctx.Value(constant.LoginType).(string) + if l.ctx.Value(constant.CtxLoginType) != nil { + req.LoginType = l.ctx.Value(constant.CtxLoginType).(string) } // Generate session id @@ -150,7 +150,8 @@ func (l *TelephoneLoginLogic) TelephoneLogin(req *types.TelephoneLoginRequest, r l.svcCtx.Config.JwtAuth.AccessExpire, jwt.WithOption("UserId", userInfo.Id), jwt.WithOption("SessionId", sessionId), - jwt.WithOption("LoginType", req.LoginType), + jwt.WithOption("identifier", req.Identifier), + jwt.WithOption("CtxLoginType", req.LoginType), ) if err != nil { l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error())) diff --git a/internal/logic/auth/telephoneResetPasswordLogic.go b/internal/logic/auth/telephoneResetPasswordLogic.go index b585c99..0848436 100644 --- a/internal/logic/auth/telephoneResetPasswordLogic.go +++ b/internal/logic/auth/telephoneResetPasswordLogic.go @@ -110,8 +110,8 @@ func (l *TelephoneResetPasswordLogic) TelephoneResetPassword(req *types.Telephon // Don't fail register if device binding fails, just log the error } } - if l.ctx.Value(constant.LoginType) != nil { - req.LoginType = l.ctx.Value(constant.LoginType).(string) + if l.ctx.Value(constant.CtxLoginType) != nil { + req.LoginType = l.ctx.Value(constant.CtxLoginType).(string) } // Generate session id sessionId := uuidx.NewUUID().String() @@ -122,7 +122,8 @@ func (l *TelephoneResetPasswordLogic) TelephoneResetPassword(req *types.Telephon l.svcCtx.Config.JwtAuth.AccessExpire, jwt.WithOption("UserId", userInfo.Id), jwt.WithOption("SessionId", sessionId), - jwt.WithOption("LoginType", req.LoginType), + jwt.WithOption("identifier", req.Identifier), + jwt.WithOption("CtxLoginType", req.LoginType), ) if err != nil { l.Errorw("[UserLogin] token generate error", logger.Field("error", err.Error())) diff --git a/internal/logic/auth/telephoneUserRegisterLogic.go b/internal/logic/auth/telephoneUserRegisterLogic.go index 5fad294..64c1dec 100644 --- a/internal/logic/auth/telephoneUserRegisterLogic.go +++ b/internal/logic/auth/telephoneUserRegisterLogic.go @@ -152,8 +152,8 @@ func (l *TelephoneUserRegisterLogic) TelephoneUserRegister(req *types.TelephoneR // Don't fail register if device binding fails, just log the error } } - if l.ctx.Value(constant.LoginType) != nil { - req.LoginType = l.ctx.Value(constant.LoginType).(string) + if l.ctx.Value(constant.CtxLoginType) != nil { + req.LoginType = l.ctx.Value(constant.CtxLoginType).(string) } // Generate session id sessionId := uuidx.NewUUID().String() @@ -164,7 +164,8 @@ func (l *TelephoneUserRegisterLogic) TelephoneUserRegister(req *types.TelephoneR l.svcCtx.Config.JwtAuth.AccessExpire, jwt.WithOption("UserId", userInfo.Id), jwt.WithOption("SessionId", sessionId), - jwt.WithOption("LoginType", req.LoginType), + jwt.WithOption("identifier", req.Identifier), + jwt.WithOption("CtxLoginType", req.LoginType), ) if err != nil { l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error())) diff --git a/internal/logic/auth/userLoginLogic.go b/internal/logic/auth/userLoginLogic.go index deecaae..bf2ed2d 100644 --- a/internal/logic/auth/userLoginLogic.go +++ b/internal/logic/auth/userLoginLogic.go @@ -93,8 +93,8 @@ func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Log // Don't fail login if device binding fails, just log the error } } - if l.ctx.Value(constant.LoginType) != nil { - req.LoginType = l.ctx.Value(constant.LoginType).(string) + if l.ctx.Value(constant.CtxLoginType) != nil { + req.LoginType = l.ctx.Value(constant.CtxLoginType).(string) } // Generate session id sessionId := uuidx.NewUUID().String() @@ -105,7 +105,8 @@ func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Log l.svcCtx.Config.JwtAuth.AccessExpire, jwt.WithOption("UserId", userInfo.Id), jwt.WithOption("SessionId", sessionId), - jwt.WithOption("LoginType", req.LoginType), + jwt.WithOption("identifier", req.Identifier), + jwt.WithOption("CtxLoginType", req.LoginType), ) if err != nil { l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error())) diff --git a/internal/logic/auth/userRegisterLogic.go b/internal/logic/auth/userRegisterLogic.go index 0198911..56773a5 100644 --- a/internal/logic/auth/userRegisterLogic.go +++ b/internal/logic/auth/userRegisterLogic.go @@ -138,8 +138,8 @@ func (l *UserRegisterLogic) UserRegister(req *types.UserRegisterRequest) (resp * // Don't fail register if device binding fails, just log the error } } - if l.ctx.Value(constant.LoginType) != nil { - req.LoginType = l.ctx.Value(constant.LoginType).(string) + if l.ctx.Value(constant.CtxLoginType) != nil { + req.LoginType = l.ctx.Value(constant.CtxLoginType).(string) } // Generate session id sessionId := uuidx.NewUUID().String() @@ -149,8 +149,9 @@ func (l *UserRegisterLogic) UserRegister(req *types.UserRegisterRequest) (resp * time.Now().Unix(), l.svcCtx.Config.JwtAuth.AccessExpire, jwt.WithOption("UserId", userInfo.Id), + jwt.WithOption("identifier", req.Identifier), jwt.WithOption("SessionId", sessionId), - jwt.WithOption("LoginType", req.LoginType), + jwt.WithOption("CtxLoginType", req.LoginType), ) if err != nil { l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error())) diff --git a/internal/middleware/deviceMiddleware.go b/internal/middleware/deviceMiddleware.go index b66ccb0..cd91a58 100644 --- a/internal/middleware/deviceMiddleware.go +++ b/internal/middleware/deviceMiddleware.go @@ -42,11 +42,11 @@ func DeviceMiddleware(srvCtx *svc.ServiceContext) func(c *gin.Context) { ctx := c.Request.Context() if ctx.Value(constant.CtxKeyUser) == nil && c.GetHeader("Login-Type") != "" { - ctx = context.WithValue(ctx, constant.LoginType, c.GetHeader("Login-Type")) + ctx = context.WithValue(ctx, constant.CtxLoginType, c.GetHeader("Login-Type")) c.Request = c.Request.WithContext(ctx) } - loginType, ok := ctx.Value(constant.LoginType).(string) + loginType, ok := ctx.Value(constant.CtxLoginType).(string) if !ok || loginType != "device" { c.Next() return diff --git a/pkg/constant/context.go b/pkg/constant/context.go index 45c7f86..b137584 100644 --- a/pkg/constant/context.go +++ b/pkg/constant/context.go @@ -8,5 +8,6 @@ const ( CtxKeyRequestHost CtxKey = "requestHost" CtxKeyPlatform CtxKey = "platform" CtxKeyPayment CtxKey = "payment" - LoginType CtxKey = "loginType" + CtxLoginType CtxKey = "loginType" + CtxKeyIdentifier CtxKey = "identifier" )