简体   繁体   English

Flutter Firebase 即使发送和输入的 OTP 相同,OTP 验证仍失败

[英]Flutter Firebase OTP verification fails even though both sent and entered OTP are the same

I am trying to implement OTP verification in my Flutter app using Firebase, but I am facing an issue where the verification fails even though both the sent and entered OTP are the same.我正在尝试使用 Firebase 在我的 Flutter 应用程序中实施 OTP 验证,但我面临验证失败的问题,即使发送和输入的 OTP 相同。

I am using the following function to verify the OTP:我正在使用以下 function 来验证 OTP:

void verifyOtp({
    required BuildContext context,
    required String verificationId,
    required String userOtp,
    required Function onSuccess,
  }) async {
    _isLoading = true;
    notifyListeners();
    try {
      PhoneAuthCredential creds = PhoneAuthProvider.credential(
          verificationId: verificationId, smsCode: userOtp);
      User? user = (await _firebaseAuth.signInWithCredential(creds)).user!;
      print('signwithcredential passed');
      if (user != null) {
        _uid = user.uid;
        onSuccess();
      }
    } on FirebaseAuthException catch (e) {
      print('failed Userotp: $userOtp');
      showSnackBar(context, e.message.toString());
      _isLoading = false;
      notifyListeners();
    }
  }

The error I am getting is "FirebaseAuthException: sms code has expired please resend the verification code to verify again."我得到的错误是“FirebaseAuthException:短信代码已过期请重新发送验证码以再次验证。”

I am new to Flutter and Firebase, so any help in fixing this problem would be greatly appreciated.我是 Flutter 和 Firebase 的新手,所以如果能帮助解决这个问题,我将不胜感激。

Here are couple of things I have found maybe they will work:以下是我发现的一些可能会起作用的东西:

  • Check the Firebase Console to ensure that the phone number being verified is correctly set up and that the SMS code is being sent to the right number.检查 Firebase 控制台以确保被验证的电话号码设置正确并且短信代码发送到正确的号码。

  • Make sure that the phone number is being passed in the correct format.确保以正确的格式传递电话号码。

  • FirebaseAuthException is being caught correctly. FirebaseAuthException 被正确捕获。 Make sure the message inside the catch block is being logged or displayed to the user.确保 catch 块内的消息被记录或显示给用户。

  • Ensure that the dependencies and packages are correctly installed and imported.确保依赖项和包已正确安装和导入。

The solution is itself an error you are getting.解决方案本身就是您遇到的错误。 You have to enter the code before it expire.您必须在代码过期前输入代码。 Try to verify the otp as soon as you get the code as it's saying " I/flutter ( 9598): error on firebaseAuthexception: [firebase_auth/session-expired] The sms code has expired. Please re-send the verification code to try again. ".收到代码后立即尝试验证 otp,因为它说“ I/flutter ( 9598): error on firebaseAuthexception: [firebase_auth/session-expired] 短信代码已过期。请重新发送验证代码以重试. ”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM