【问题标题】:Resend OTP code Firebase phone authentication in Flutter在 Flutter 中重新发送 OTP 代码 Firebase 电话身份验证
【发布时间】:2020-07-22 17:25:20
【问题描述】:

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

有关于 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
    );


  }

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

https://stackoverflow.com/a/44688838/10114772

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
}

但那不是颤抖的,请有人看看!

【问题讨论】:

  • Flutter 的 Firebase API 与 Android 没有太大区别。如果您看到一个 Android 解决方案,如果您只是在 API 文档中搜索它,那么很可能有一个 Flutter 等价物。
  • 你可能是对的,但我想不通尝试了很多线程和网站
  • 好吧,如果您编辑问题以包含有关 Android 解决方案的一些信息,那么其他人可能很容易找到 Flutter 等效项。
  • @DougStevenson 我已经包含了 android 解决方案

标签: flutter dart firebase-authentication


【解决方案1】:

所以在按照我的阅读文档后,回忆

verifyPhoneNumber()

方法将重新发送 OTP 代码。

【讨论】:

  • 嘿,我试过你的解决方案 - 它确实会发送 OTP 代码,但只有在我收到之前的代码后等待一段时间,你知道它等待多长时间吗? (我取消了自动检索以检查此功能,因此当前超时为 0)。
【解决方案2】:

在第一次发送otp时将forceResendingToken的值记录为codeSent回调中的resendToken,然后重新发送otp,在auth.verifyPhoneNumber()函数中添加一个额外的参数forceResendingToken: resendToken。

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,我只是禁用了重新发送按钮,直到超时并显示相同持续时间的倒数计时器,超时后您可以启用重新发送按钮,用户可以重新发送代码。它对我有用。

    【讨论】:

      猜你喜欢
      • 2021-05-03
      • 1970-01-01
      • 2021-06-27
      • 2019-12-04
      • 2021-06-10
      • 2021-06-11
      • 2019-12-01
      • 1970-01-01
      • 2021-02-12
      相关资源
      最近更新 更多