diff --git a/internal/config/cacheKey.go b/internal/config/cacheKey.go index 655ce55..b2b290b 100644 --- a/internal/config/cacheKey.go +++ b/internal/config/cacheKey.go @@ -39,6 +39,9 @@ const VerifyCodeConfigKey = "system:verify_code_config" // SessionIdKey cache session key const SessionIdKey = "auth:session_id" +// DeviceCacheKeyKey cache session key +const DeviceCacheKeyKey = "auth:device_identifier" + // GlobalConfigKey Global Config Key const GlobalConfigKey = "system:global_config" diff --git a/internal/logic/auth/deviceLoginLogic.go b/internal/logic/auth/deviceLoginLogic.go index 2e2b6ae..4e592f4 100644 --- a/internal/logic/auth/deviceLoginLogic.go +++ b/internal/logic/auth/deviceLoginLogic.go @@ -125,6 +125,17 @@ func (l *DeviceLoginLogic) DeviceLogin(req *types.DeviceLoginRequest) (resp *typ return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "set session id error: %v", err.Error()) } + // Store device id in redis + + deviceCacheKey := fmt.Sprintf("%v:%v", config.DeviceCacheKeyKey, req.Identifier) + if err = l.svcCtx.Redis.Set(l.ctx, deviceCacheKey, sessionId, time.Duration(l.svcCtx.Config.JwtAuth.AccessExpire)*time.Second).Err(); err != nil { + l.Errorw("set device id error", + logger.Field("user_id", userInfo.Id), + logger.Field("error", err.Error()), + ) + return nil, errors.Wrapf(xerr.NewErrCode(xerr.ERROR), "set device id error: %v", err.Error()) + } + loginStatus = true return &types.LoginResponse{ Token: token, diff --git a/internal/logic/public/user/unbindDeviceLogic.go b/internal/logic/public/user/unbindDeviceLogic.go index 57218cc..e4f519b 100644 --- a/internal/logic/public/user/unbindDeviceLogic.go +++ b/internal/logic/public/user/unbindDeviceLogic.go @@ -64,9 +64,14 @@ func (l *UnbindDeviceLogic) UnbindDevice(req *types.UnbindDeviceRequest) error { if err != nil { return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete device online record err: %v", err) } - sessionId := l.ctx.Value(constant.CtxKeySessionID) - sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId) - l.svcCtx.Redis.Del(l.ctx, sessionIdCacheKey) + + //remove device cache + deviceCacheKey := fmt.Sprintf("%v:%v", config.DeviceCacheKeyKey, deleteDevice.Identifier) + if sessionId, err := l.svcCtx.Redis.Get(l.ctx, deviceCacheKey).Result(); err == nil && sessionId != "" { + _ = l.svcCtx.Redis.Del(l.ctx, deviceCacheKey).Err() + sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId) + _ = l.svcCtx.Redis.Del(l.ctx, sessionIdCacheKey).Err() + } return nil }) }