Flutter Firebase电话身份验证不起作用

时间:2019-12-01 18:54:37

标签: android firebase flutter dart firebase-authentication

电话身份验证失败,但有以下异常:

PlatformException(ERROR_SESSION_EXPIRED,短信代码已过期。请重新发送验证码以重试。,空)

但是,如果我使用手机上以外的其他电话号码,则可以使用。我已经将Play商店中的SHA-1和SHA-256指纹都添加到了Firebase中,并且还替换了google-services.json。

这是我的代码:

 void _verifyPhoneNumber() async {
    setState(() {
       isVerified=true; 
      });
    setState(() {
      _message = '';
    });
    final PhoneVerificationCompleted verificationCompleted =
        (AuthCredential phoneAuthCredential) {
      _auth.signInWithCredential(phoneAuthCredential);
      setState(() {
        _message = 'Received phone auth credential: $phoneAuthCredential';

      });
    };

    final PhoneVerificationFailed verificationFailed =
        (AuthException authException) {
        _message =
            'Phone number verification failed';

    };

    final PhoneCodeSent codeSent =
        (String verificationId, [int forceResendingToken]) async {
      _verificationId = verificationId;

    };

    final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
        (String verificationId) {
      _verificationId = verificationId;
    };

    await _auth.verifyPhoneNumber(
        phoneNumber: '+91'+_phoneNumberController.text,
        timeout: const Duration(seconds: 5),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
  }

  // Example code of how to sign in with phone.
  void _signInWithPhoneNumber() async {
    setState(() {
      isLoading=true;
    });
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: _verificationId,
      smsCode: _smsController.text,
    );
    try{
      firebaseUser =
        (await _auth.signInWithCredential(credential)).user;
    final FirebaseUser currentUser = await _auth.currentUser();
    assert(firebaseUser.uid == currentUser.uid);
      if (firebaseUser != null) {
.....

      } else {

        _message = 'Sign in failed';
        showErrorDialog();
      }

    }catch (e){
      showErrorDialog();
    }
    setState(() {
      isLoading=false;
    });
  }

3 个答案:

答案 0 :(得分:1)

不确定您的问题,但它说: ERROR_SESSION_EXPIRED,短信代码已过期,并且在_auth.verifyPhoneNumber()中,您的超时持续时间非常低。尝试 60秒

await _auth.verifyPhoneNumber(
        phoneNumber: '+91${_phoneNumberController.text}',
        timeout: Duration(seconds: 60),
        verificationCompleted: verificationCompleted,
        verificationFailed: verificationFailed,
        codeSent: codeSent,
        codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);

并且如果这样做不起作用,请查看docs

答案 1 :(得分:0)

虽然超时属性为60s,但是。 但是,ERROR_SESSION_EXPIRED收到验证码并立即粘贴后,就像您一样。 注意:仅在Android上,只需通过几个电话号码接收。

答案 2 :(得分:0)

为时已晚,但对于其他正在苦苦挣扎的人来说,这一切都是关于登录和注册文档的不完整解释。

注释_verifyPhoneNumber()函数中的signin代码,在函数中添加async,使AuthCredential变成PhoneAuthCredential

  final PhoneVerificationCompleted verificationCompleted =
    (PhoneAuthCredential phoneAuthCredential) async{
 // comment the below code for _auth.signIn and add async

 // _auth.signInWithCredential(phoneAuthCredential);
  setState(() {
    _message = 'Received phone auth credential: $phoneAuthCredential';

  });

实际上 _verifyPhoneNumber() 会检查数据库中的号码,因此您可以从此函数将用户直接重定向到主屏幕。由于 signIn 运行两次,一次在这里,一次在 signinwithPhone Number() 中,因此它给出了超时错误。