繁体   English   中英

"如何在flutter中使用firebase电话身份验证中的forceResendingToken重新发送otp"

[英]How to use the forceResendingToken in firebase phone auth in flutter to resend the otp

我正在寻找一种在 firebase verifyPhoneNumber 中重新发送OTP的方法。 我已经浏览了有关 phoneAuth 的示例,但找不到重新发送 OTP 的方法。 有一个 forceResendingToken 选项

final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      this.verificationId = verificationId;
      _smsCodeController.text = testSmsCode;
    };


await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: this._phone,
        codeAutoRetrievalTimeout: autoRetrieval,
        codeSent: smsCodeSent,
        forceResendingToken: ,//how to get this token
        timeout: const Duration(seconds: 40),
        verificationCompleted: verifSuccessful,
        verificationFailed: verifFailed);
  }

如何使用此令牌重新发送 OTP。

Firebase参考文档中

ForceResendingToken从获得onCodeSent(String, PhoneAuthProvider.ForceResendingToken) callback强制重新发送自动检索超时之前另一验证短信。

因此,在您的情况下,这将是verifyPhoneNumber()调用的PhoneCodeSent回调。

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

Firebase 发送 3 次相同的 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;
}

暂无
暂无

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

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