fix(用户): 修复邮箱验证码校验逻辑,支持多场景验证
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
Some checks failed
Build docker and publish / build (20.15.1) (push) Has been cancelled
修改邮箱验证码校验逻辑,使其支持安全场景和注册场景的验证码校验。当任一场景验证通过时即视为验证成功,提升用户体验。
This commit is contained in:
parent
14f4e351f4
commit
55b8fe813d
@ -48,26 +48,33 @@ func (l *BindEmailWithVerificationLogic) BindEmailWithVerification(req *types.Bi
|
|||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access")
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.InvalidAccess), "Invalid Access")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 校验邮箱验证码(安全场景)
|
|
||||||
type payload struct {
|
type payload struct {
|
||||||
Code string `json:"code"`
|
Code string `json:"code"`
|
||||||
LastAt int64 `json:"lastAt"`
|
LastAt int64 `json:"lastAt"`
|
||||||
}
|
}
|
||||||
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, constant.Security, req.Email)
|
var (
|
||||||
value, getErr := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
|
scenes = []string{constant.Security.String(), constant.Register.String()}
|
||||||
if getErr != nil || value == "" {
|
verified = false
|
||||||
l.Errorw("邮箱验证码校验失败", logger.Field("cacheKey", cacheKey), logger.Field("error", getErr))
|
)
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "验证码错误")
|
for _, scene := range scenes {
|
||||||
|
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, scene, req.Email)
|
||||||
|
value, getErr := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
|
||||||
|
if getErr != nil || value == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var p payload
|
||||||
|
if err := json.Unmarshal([]byte(value), &p); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if p.Code == req.Code {
|
||||||
|
_ = l.svcCtx.Redis.Del(l.ctx, cacheKey).Err()
|
||||||
|
verified = true
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
var p payload
|
if !verified {
|
||||||
if err := json.Unmarshal([]byte(value), &p); err != nil {
|
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "code error")
|
||||||
l.Errorw("邮箱验证码反序列化失败", logger.Field("error", err.Error()), logger.Field("value", value))
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "验证码错误")
|
|
||||||
}
|
}
|
||||||
if p.Code != req.Code {
|
|
||||||
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "验证码错误")
|
|
||||||
}
|
|
||||||
_ = l.svcCtx.Redis.Del(l.ctx, cacheKey).Err()
|
|
||||||
|
|
||||||
// 获取当前用户的设备标识符
|
// 获取当前用户的设备标识符
|
||||||
deviceIdentifier, err := l.getCurrentUserDeviceIdentifier(l.ctx, u.Id)
|
deviceIdentifier, err := l.getCurrentUserDeviceIdentifier(l.ctx, u.Id)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user