feat: verify code
This commit is contained in:
@@ -104,6 +104,27 @@ func smsFreqRedisKey(scene int32, mobile string) string {
|
||||
return "sms:freq:" + strconv.Itoa(int(scene)) + ":" + mobile
|
||||
}
|
||||
|
||||
func verifyAndConsumeSmsCode(ctx context.Context, scene int32, mobile, code string) (bool, error) {
|
||||
if code == utils.StringEmpty {
|
||||
return false, nil
|
||||
}
|
||||
codeKey := smsCodeRedisKey(scene, mobile)
|
||||
cached, err := redis.Client.Get(ctx, codeKey).Result()
|
||||
if err == redis.Nil {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if cached != code {
|
||||
return false, nil
|
||||
}
|
||||
if delErr := redis.Client.Del(ctx, codeKey).Err(); delErr != nil {
|
||||
return false, delErr
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func genDigitCode(length int) (string, error) {
|
||||
if length <= utils.NumberZero {
|
||||
return utils.StringEmpty, fmt.Errorf("invalid code length")
|
||||
|
||||
Reference in New Issue
Block a user