add: Automatically delete token when device is removed
This commit is contained in:
parent
fb226a0fc1
commit
d78d79fa2d
@ -39,6 +39,9 @@ const VerifyCodeConfigKey = "system:verify_code_config"
|
|||||||
// SessionIdKey cache session key
|
// SessionIdKey cache session key
|
||||||
const SessionIdKey = "auth:session_id"
|
const SessionIdKey = "auth:session_id"
|
||||||
|
|
||||||
|
// DeviceCacheKeyKey cache session key
|
||||||
|
const DeviceCacheKeyKey = "auth:device_identifier"
|
||||||
|
|
||||||
// GlobalConfigKey Global Config Key
|
// GlobalConfigKey Global Config Key
|
||||||
const GlobalConfigKey = "system:global_config"
|
const GlobalConfigKey = "system:global_config"
|
||||||
|
|
||||||
|
|||||||
@ -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())
|
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
|
loginStatus = true
|
||||||
return &types.LoginResponse{
|
return &types.LoginResponse{
|
||||||
Token: token,
|
Token: token,
|
||||||
|
|||||||
@ -64,9 +64,14 @@ func (l *UnbindDeviceLogic) UnbindDevice(req *types.UnbindDeviceRequest) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete device online record err: %v", err)
|
return errors.Wrapf(xerr.NewErrCode(xerr.DatabaseDeletedError), "delete device online record err: %v", err)
|
||||||
}
|
}
|
||||||
sessionId := l.ctx.Value(constant.CtxKeySessionID)
|
|
||||||
|
//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)
|
sessionIdCacheKey := fmt.Sprintf("%v:%v", config.SessionIdKey, sessionId)
|
||||||
l.svcCtx.Redis.Del(l.ctx, sessionIdCacheKey)
|
_ = l.svcCtx.Redis.Del(l.ctx, sessionIdCacheKey).Err()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user