fix(auth): 修复邮件验证码逻辑,支持多种场景验证
All checks were successful
Build docker and publish / build (20.15.1) (push) Successful in 4m42s

修改邮件验证码校验逻辑,使其能够处理注册和安全验证等多种场景。移除不再需要的用户绑定检查,简化代码逻辑。
This commit is contained in:
shanshanzhong 2025-12-31 02:09:31 -08:00
parent 4ffccd5ad8
commit 5598181a48
2 changed files with 21 additions and 17 deletions

View File

@ -60,23 +60,29 @@ func (l *EmailLoginLogic) EmailLogin(req *types.EmailLoginRequest) (resp *types.
// So the frontend probably sends type="login" (or similar).
// Let's check `constant` package for available types? I don't see it.
// Assuming `constant.Security` for generic verification.
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, constant.Security, req.Email)
scenes := []string{constant.Security.String(), constant.Register.String()}
var verified bool
var cacheKeyUsed string
var payload common.CacheKeyPayload
for _, scene := range scenes {
cacheKey := fmt.Sprintf("%s:%s:%s", config.AuthCodeCacheKey, scene, req.Email)
value, err := l.svcCtx.Redis.Get(l.ctx, cacheKey).Result()
if err != nil {
l.Errorw("Verification code error (Redis get)", logger.Field("cacheKey", cacheKey), logger.Field("error", err.Error()))
if err != nil || value == "" {
continue
}
if err := json.Unmarshal([]byte(value), &payload); err != nil {
continue
}
if payload.Code == req.Code {
verified = true
cacheKeyUsed = cacheKey
break
}
}
if !verified {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "verification code error or expired")
}
var payload common.CacheKeyPayload
if err := json.Unmarshal([]byte(value), &payload); err != nil {
l.Errorw("Unmarshal error", logger.Field("error", err.Error()), logger.Field("value", value))
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "verification code error")
}
if payload.Code != req.Code {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.VerifyCodeError), "verification code mismatch")
}
// Delete code after use? Or keep it? Usually delete.
l.svcCtx.Redis.Del(l.ctx, cacheKey)
l.svcCtx.Redis.Del(l.ctx, cacheKeyUsed)
// Check User

View File

@ -78,8 +78,6 @@ func (l *SendEmailCodeLogic) SendEmailCode(req *types.SendCodeRequest) (resp *ty
}
if constant.ParseVerifyType(req.Type) == constant.Register && m.Id > 0 {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserExist), "mobile already bind")
} else if constant.ParseVerifyType(req.Type) == constant.Security && m.Id == 0 {
return nil, errors.Wrapf(xerr.NewErrCode(xerr.UserNotExist), "mobile not bind")
}
var payload CacheKeyPayload