feat: 添加种子数据初始化功能,重构多个处理程序以简化错误响应和用户验证
This commit is contained in:
@@ -91,21 +91,36 @@ func VerifyCode(ctx context.Context, redisClient *redis.Client, email, code, cod
|
||||
return nil
|
||||
}
|
||||
|
||||
// 检查是否被锁定
|
||||
locked, ttl, err := CheckVerifyLocked(ctx, redisClient, email, codeType)
|
||||
if err == nil && locked {
|
||||
return fmt.Errorf("验证码错误次数过多,请在 %d 分钟后重试", int(ttl.Minutes())+1)
|
||||
}
|
||||
|
||||
codeKey := fmt.Sprintf("verification:code:%s:%s", codeType, email)
|
||||
|
||||
// 从Redis获取验证码
|
||||
storedCode, err := redisClient.Get(ctx, codeKey)
|
||||
if err != nil {
|
||||
// 记录失败尝试
|
||||
RecordVerifyFailure(ctx, redisClient, email, codeType)
|
||||
return fmt.Errorf("验证码已过期或不存在")
|
||||
}
|
||||
|
||||
// 验证验证码
|
||||
if storedCode != code {
|
||||
// 记录失败尝试
|
||||
count, _ := RecordVerifyFailure(ctx, redisClient, email, codeType)
|
||||
remaining := MaxVerifyAttempts - count
|
||||
if remaining > 0 {
|
||||
return fmt.Errorf("验证码错误,还剩 %d 次尝试机会", remaining)
|
||||
}
|
||||
return fmt.Errorf("验证码错误")
|
||||
}
|
||||
|
||||
// 验证成功,删除验证码
|
||||
// 验证成功,删除验证码和失败计数
|
||||
_ = redisClient.Del(ctx, codeKey)
|
||||
_ = ClearVerifyAttempts(ctx, redisClient, email, codeType)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user