dart 如何在flutter中使用firebase phone auth中的forceResendingToken重发otp

xxls0lw8  于 2023-05-11  发布在  Flutter
关注(0)|答案(3)|浏览(103)

我正在寻找一种方法来重新发送OTP在firebase verifyPhoneNumber在Flutter。我已经通过了关于phoneAuth的example,但找不到重新发送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。

qojgxg4l

qojgxg4l1#

Firebase参考文档:
onCodeSent(String, PhoneAuthProvider.ForceResendingToken) callback获取的ForceResendingToken,在自动检索超时前强制重发另一条验证短信。
因此,在您的示例中,这将是verifyPhoneNumber()调用的PhoneCodeSent回调。

xhv8bpkk

xhv8bpkk2#

您必须记录来自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;
}
f87krz0w

f87krz0w3#

当重新发送otp时,我每次在重新发送时都接收到相同的otp

相关问题