From 236fa6c4e6f21d2535d1b69972432f26d9845e26 Mon Sep 17 00:00:00 2001 From: shanshanzhong Date: Wed, 26 Nov 2025 22:57:10 -0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E5=9C=A8=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E4=B8=AD=E6=B7=BB=E5=8A=A0=E4=BC=9A=E8=AF=9D?= =?UTF-8?q?=E9=99=90=E5=88=B6=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 在LoginResponse结构体中新增Limit字段,用于返回用户最大会话数限制 修复userLoginLogic.go和telephoneLoginLogic.go中的缩进问题 --- internal/logic/auth/deviceLoginLogic.go | 1 + internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go | 5 ++++- internal/logic/auth/telephoneLoginLogic.go | 7 ++++--- internal/logic/auth/userLoginLogic.go | 7 ++++--- internal/types/types.go | 1 + 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/internal/logic/auth/deviceLoginLogic.go b/internal/logic/auth/deviceLoginLogic.go index 52cdce5..b2909ed 100644 --- a/internal/logic/auth/deviceLoginLogic.go +++ b/internal/logic/auth/deviceLoginLogic.go @@ -166,6 +166,7 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ loginStatus = true return &types.LoginResponse{ Token: token, + Limit: l.svcCtx.Config.JwtAuth.MaxSessionsPerUser, }, nil } diff --git a/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go b/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go index 8bebe11..da7bd6c 100644 --- a/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go +++ b/internal/logic/auth/oauth/oAuthLoginGetTokenLogic.go @@ -79,7 +79,10 @@ func (l *OAuthLoginGetTokenLogic) OAuthLoginGetToken(req *types.OAuthLoginGetTok } loginStatus = true - return &types.LoginResponse{Token: token}, nil + return &types.LoginResponse{ + Token: token, + Limit: l.svcCtx.Config.JwtAuth.MaxSessionsPerUser, + }, nil } func (l *OAuthLoginGetTokenLogic) google(req *types.OAuthLoginGetTokenRequest, requestID, ip, userAgent string) (*user.User, error) { diff --git a/internal/logic/auth/telephoneLoginLogic.go b/internal/logic/auth/telephoneLoginLogic.go index ca2b62b..5af5364 100644 --- a/internal/logic/auth/telephoneLoginLogic.go +++ b/internal/logic/auth/telephoneLoginLogic.go @@ -156,9 +156,9 @@ func (l *TelephoneLoginLogic) TelephoneLogin(req *types.TelephoneLoginRequest, r l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error())) return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "token generate error: %v", err.Error()) } - if err = l.svcCtx.EnforceUserSessionLimit(l.ctx, userInfo.Id, sessionId, l.svcCtx.Config.JwtAuth.MaxSessionsPerUser); err != nil { - return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "enforce session limit error: %v", err.Error()) - } + if err = l.svcCtx.EnforceUserSessionLimit(l.ctx, userInfo.Id, sessionId, l.svcCtx.Config.JwtAuth.MaxSessionsPerUser); err != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "enforce session limit error: %v", err.Error()) + } sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId) if err = l.svcCtx.Redis.Set(l.ctx, sessionIdCacheKey, userInfo.Id, time.Duration(l.svcCtx.Config.JwtAuth.AccessExpire)*time.Second).Err(); err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "set session id error: %v", err.Error()) @@ -166,5 +166,6 @@ func (l *TelephoneLoginLogic) TelephoneLogin(req *types.TelephoneLoginRequest, r loginStatus = true return &types.LoginResponse{ Token: token, + Limit: l.svcCtx.Config.JwtAuth.MaxSessionsPerUser, }, nil } diff --git a/internal/logic/auth/userLoginLogic.go b/internal/logic/auth/userLoginLogic.go index 36fad03..fee6216 100644 --- a/internal/logic/auth/userLoginLogic.go +++ b/internal/logic/auth/userLoginLogic.go @@ -111,9 +111,9 @@ func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Log l.Logger.Error("[UserLogin] token generate error", logger.Field("error", err.Error())) return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "token generate error: %v", err.Error()) } - if err = l.svcCtx.EnforceUserSessionLimit(l.ctx, userInfo.Id, sessionId, l.svcCtx.Config.JwtAuth.MaxSessionsPerUser); err != nil { - return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "enforce session limit error: %v", err.Error()) - } + if err = l.svcCtx.EnforceUserSessionLimit(l.ctx, userInfo.Id, sessionId, l.svcCtx.Config.JwtAuth.MaxSessionsPerUser); err != nil { + return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "enforce session limit error: %v", err.Error()) + } sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId) if err = l.svcCtx.Redis.Set(l.ctx, sessionIdCacheKey, userInfo.Id, time.Duration(l.svcCtx.Config.JwtAuth.AccessExpire)*time.Second).Err(); err != nil { return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "set session id error: %v", err.Error()) @@ -121,5 +121,6 @@ func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Log loginStatus = true return &types.LoginResponse{ Token: token, + Limit: l.svcCtx.Config.JwtAuth.MaxSessionsPerUser, }, nil } diff --git a/internal/types/types.go b/internal/types/types.go index 31d7191..da50e3d 100644 --- a/internal/types/types.go +++ b/internal/types/types.go @@ -1219,6 +1219,7 @@ type LoginLog struct { type LoginResponse struct { Token string `json:"token"` + Limit int64 `json:"limit"` } type MessageLog struct {