在 Flutter 中重新发送 OTP 代码 Firebase 电话身份验证

MR_*_*DEV 7 dart firebase-authentication flutter

这可能是重复的,但任何其他线程都没有为我提供正确的答案。

有关于 android 原生语言的答案,但没有关于 Flutter(dart) 的答案。

我有以下有效方法,但如果我想将 OTP 重新发送到用户电话号码,我该怎么做?只需一个简单的示例代码可能会有所帮助。

  Future signInWithPhone(String phone, BuildContext context) async {

    // This triggers if verification passes
    final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) async {
      Navigator.of(context).pop();

      AuthResult result = await _auth.signInWithCredential(credential);

      FirebaseUser user = result.user;

      if(user != null){
        Navigator.push(context, MaterialPageRoute(
          builder: (context) => HomeScreen(user: user,)
        ));
      }else{
        showDialog(
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text("Alert Dialog"),
              content: Text('Error'),
            );
          }
        );
      }
    };

    // This triggers if verification fails
    PhoneVerificationFailed verificationFailed = (AuthException exception) {
      toast(exception.message, 'long');
    };

    // This is to send code to the user But i dont know how to resend
    final PhoneCodeSent codeSent = (String verificationId, [int forceResendingToken]) {
        var route = MaterialPageRoute(
          builder: (BuildContext context) => LoginPhoneVerify(verificationId, phone)
        );

        Navigator.of(context).push(route);
    };

    // The main function
    await _auth.verifyPhoneNumber(
      phoneNumber: phone,
      timeout: Duration(seconds: 0),
      verificationCompleted: verificationCompleted,
      verificationFailed: verificationFailed,        
      codeSent: codeSent,
      codeAutoRetrievalTimeout: null
    );


  }
Run Code Online (Sandbox Code Playgroud)

我在以下线程中找到了适用于 android 的东西:

/sf/answers/3128218691/

private void resendVerificationCode(String phoneNumber, PhoneAuthProvider.ForceResendingToken token) {
    PhoneAuthProvider.getInstance().verifyPhoneNumber(
            phoneNumber,        // Phone number to verify
            60,                 // Timeout duration
            TimeUnit.SECONDS,   // Unit of timeout
            this,               // Activity (for callback binding)
            mCallbacks,         // OnVerificationStateChangedCallbacks
            token);             // ForceResendingToken from callbacks
}
Run Code Online (Sandbox Code Playgroud)

但这不是为了颤振,请有人看一下!

小智 6

您必须记录codesent回调中resendToken的值并将其传递给forceresendingtoken。另外,内置超时持续时间为 30 秒,因此请确保在超时秒数后点击重新发送按钮,您可以使用计时器按钮包,在这种情况下它非常方便。

Firebase 发送 3x 相同的 SMS 代码,然后将其更改为不同的。

    String _verificationId = "";
    int? _resendToken;

    Future<bool> sendOTP({required String phone}) async {
      await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: phone,
        verificationCompleted: (PhoneAuthCredential credential) {},
        verificationFailed: (FirebaseAuthException e) {},
        codeSent: (String verificationId, int? resendToken) async {
        _verificationId = verificationId;
        _resendToken = resendToken;
        },
        timeout: const Duration(seconds: 25),
        forceResendingToken: _resendToken,
        codeAutoRetrievalTimeout: (String verificationId) {
        verificationId = _verificationId;
      },
     );
   debugPrint("_verificationId: $_verificationId");
   return true;
  }
Run Code Online (Sandbox Code Playgroud)


MR_*_*DEV 4

因此,在根据我的说法查看文档后,回想起

verifyPhoneNumber()
Run Code Online (Sandbox Code Playgroud)

方法将重新发送 OTP 代码。