如何使用 firebase_auth(电话身份验证)在 Flutter 应用程序中添加短信自动验证?

pro*_*ion 5 dart firebase-authentication flutter

在我的 flutter 应用程序中,我使用Firebase电话身份验证进行登录。总而言之,它完美地工作。通过使用我当前的代码,我必须在收到代码时明确添加短信代码。

如何在我的 Flutter 应用程序中自动进行此短信验证?

    Future<void> _sendCodeToPhoneNumber() async {

    final String phone = country + phoneNumberController.text;

    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential credential) {
      setState(() {
        print(
            'Inside _sendCodeToPhoneNumber: signInWithPhoneNumber auto succeeded: $credential');
      });
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
      setState(() {
        print(
            'Phone number verification failed. Code: ${authException.code}. Message: ${authException.message}');
      });
    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      this._verificationId = verificationId;
      print("code sent to " + phone);
      setState(() {
        this.status = AuthStatus.SMS_AUTH;
      });
    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      this._verificationId = verificationId;

      print("time out");
    };

    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: phone,
        timeout: const Duration(seconds: 1),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }
Run Code Online (Sandbox Code Playgroud)

小智 0

不知道您的回答是否正确,您是否要自动验证短信代码?

这仅在 Android 中可行(当自动验证过程发生时,函数 verifyCompleted 返回一个对象凭据),但您应该始终通过 UI 进行回退来验证 SMS 代码,因为 Android 并不总是可以自动验证 SMS 代码。

此外,您还需要代码来生成凭据,然后执行登录/注册过程。

如果这回答了您的问题,请告诉我。