【问题标题】:Flutter Firebase Phone Authentication not workingFlutter Firebase 电话身份验证不起作用
【发布时间】:2020-03-26 11:18:32
【问题描述】:

电话身份验证失败,出现以下异常:

PlatformException(ERROR_SESSION_EXPIRED,短信验证码已过期,请重新发送验证码重试,null)

但如果我使用的电话号码与我手机上的电话号码不同,它会起作用。我已将 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;
    });
  }

【问题讨论】:

标签: android firebase flutter dart firebase-authentication


【解决方案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

【讨论】:

  • 奇怪的是,只有当 sim 在同一设备中时才会发生错误
  • 很抱歉,您可以查看post
  • 不正确,因为文档说:If you specified a positive value less than 30 seconds, library will default to 30 seconds.
  • 超时不是短信有效期。请阅读文档。这是自动验证超时。文档:“您愿意等待库完成 SMS 自动检索的最长时间。允许的最大值为 2 分钟”
【解决方案2】:

对,虽然 timeout 属性是 60s。 但是在收到验证码并立即粘贴验证码后,ERROR_SESSION_EXPIRED 喜欢你。 注意:仅在 Android 上,只需接收几个电话号码即可。

【讨论】:

    【解决方案3】:

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

    在 _verifyPhoneNumber() 函数中注释登录代码,在函数中添加 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() 会检查数据库中的号码,因此您可以通过此功能将用户直接重定向到主屏幕。由于登录运行两次,一次在此处,一次在 signinwithPhone Number() 中,因此会出现超时错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-06-17
      • 2019-08-05
      • 2021-02-12
      • 2020-01-23
      • 1970-01-01
      相关资源
      最近更新 更多