【问题标题】:The verification ID used to create the phone auth credential is invalid in flutter用于创建手机身份验证凭据的验证ID在flutter中无效
【发布时间】:2020-12-19 11:30:50
【问题描述】:

我正在尝试在我的颤振应用程序中实现 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';
                });
              },
              );

但我总是收到这个错误:

[firebase_auth/invalid-verification-id] The verification ID used to create the phone auth credential is invalid.

有什么帮助吗??

【问题讨论】:

    标签: android ios firebase flutter firebase-authentication


    【解决方案1】:

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

    var _credential = PhoneAuthProvider.credential(verificationId: actualCode, smsCode: smsCodeController.text);
    

    您已经从参数authCredential 中获得了一个凭据,因此只需使用它登录即可。

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

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

    【讨论】:

    • 不是这样的!!!每当发出身份验证请求时,都会创建 VerificationId。而且每次都是独一无二的
    • @AliYarKhan 如何避免这个错误?任何帮助
    • @RajendraAVerma 我没有找到任何解决方案:-/
    • 当您的应用在后台读取短信代码时,请确保您传递了真实的 verifyId 值。就我而言,我在 codeSent 回调后将 verifyId 作为全局变量作为 _verificationId 并在 signInWithSmsCode(String smsCode) 中使用它,如下所示。 AuthCredential authCredential = PhoneAuthProvider.credential(smsCode: smsCode, verifyId: _verificationId);它正在工作。
    猜你喜欢
    • 2020-12-07
    • 2015-12-20
    • 2014-10-15
    • 2021-05-12
    • 2021-06-05
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多