用于创建电话身份验证凭证的验证 ID 在 flutter 中无效

Ahm*_*gdi 8 android ios firebase firebase-authentication flutter

我正在尝试在我的 flutter 应用程序中实现 Firebase 电话身份验证,我使用了软件包:Firebase_coreFirebase_auth,这是我使用的代码:

          FirebaseAuth _auth = FirebaseAuth.instance;
          _auth.verifyPhoneNumber(
              phoneNumber: '+2$mobile',
              timeout: Duration(seconds: 60),
              verificationCompleted: (AuthCredential authCredential){
                var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);
                _auth.signInWithCredential(_credential).then((UserCredential result) async {
                  pr.hide();
                  setState(() {
                    status = 'Authentication successful';
                  });
//The rest of my success code
                }).catchError((e){
                  print(e);
                  Navigator.of(context).pushAndRemoveUntil(
                      MaterialPageRoute(
                          builder: (context) => Welcome()),
                          (Route<dynamic> route) => false);                
};
              },
              verificationFailed: (FirebaseAuthException  authException){
                print(authException.message);
              },
              codeSent: (String verificationId, [int forceResendingToken]){
                setState(() {
                  actualCode = verificationId;
                  status = 'Code sent';
                });
              },
              codeAutoRetrievalTimeout: (String verificationId){
                verificationId = verificationId;
                print(verificationId);
                setState(() {
                  status = 'Auto retrieval timeout';
                });
              },
              );
Run Code Online (Sandbox Code Playgroud)

但我总是收到这个错误:

[firebase_auth/invalid-verification-id] The verification ID used to create the phone auth credential is invalid.
Run Code Online (Sandbox Code Playgroud)

有什么帮助吗?

Ben*_*min 3

我不确定为什么这条线存在:

var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);
Run Code Online (Sandbox Code Playgroud)

您已经从参数中获得了凭据authCredential,因此只需使用该凭据登录即可,因为它是有效的。

verifyPhoneNumber方法的工作方式FirebaseAuth如下:

  • verificationFailed:验证时抛出的任何错误的回调
  • verificationCompleted:如果不需要短信代码来验证(谷歌知道这是一个有效的号码)
  • codeSent:发送代码时触发,这是您让用户输入短信代码并AuthCredential使用提供的verificationId短信代码创建短信的功能。