【问题标题】:Firebase not sending OTP to the phone number in deviceFirebase 未向设备中的电话号码发送 OTP
【发布时间】:2020-05-26 02:54:10
【问题描述】:

我已经在我的 Flutter 应用程序中实现了 firebase otp

  • 当我在我的设备上安装应用程序并在该设备上请求相同的号码时,它不会向我发送消息。但是当我更改号码时,它会发送消息

  • 于是我更换了设备,尝试发送之前的号码,发送成功。然后我在第二台设备上使用了这个号码,它没有发送消息

* 当我在设备上使用相同号码时,它不会发送 OTP

这是发送 otp 的代码

  Future<void> verifyPhone() async {
    final PhoneCodeAutoRetrievalTimeout autoRetrieve = (String verID) {
      this.verificationId = verID;
    };
    final PhoneCodeSent smsCodeSent = (String verId, [int forceCodeResend]) {
      this.verificationId = verId;
    };
    final PhoneVerificationCompleted verifiedSuccess =
        (AuthCredential phoneAuthCredential) {
      print('verified');
    };
    final PhoneVerificationFailed verifyFailed = (AuthException exception) {
      print('${exception.message}');
    };

    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: widget.phone,
        timeout: const Duration(seconds: 5),
        verificationCompleted: verifiedSuccess,
        verificationFailed: verifyFailed,
        codeSent: smsCodeSent,
        codeAutoRetrievalTimeout: autoRetrieve);

    log("OTP sent");
  }

这是验证 OTP 的代码

signIn() async{
    final AuthCredential credential = PhoneAuthProvider.getCredential(
      verificationId: verificationId,
      smsCode: enteredOtp,
    );
    await FirebaseAuth.instance.signInWithCredential(credential).then((user) {
      //SOME CODE HERE
    }).catchError((e) {
      showAlert(
        context: context,
        title: "Empty or Invalid OTP",
      );
      log("Invalid OTP");
    });
  }

这里有什么问题?如何解决此问题并在设备中获取相同号码的 otp。

【问题讨论】:

  • 您在日志中遇到什么异常?

标签: firebase flutter dart firebase-authentication one-time-password


【解决方案1】:

事实上,我相信这实际上是预期的,也是预期的用途。 (或接近它)好像 Firebase 可以通过推送通知确认手机身份,它甚至不发送文本,因为它已经过验证。 (通常仅在验证手机一次时才会发生)。但是,如果您将其发送到设备正在连接的电话号码以外的电话号码,则它无法使用系统中以前的验证ID。

所以我建议检查代码的哪些部分起作用而不是。检查代码和授权是否失败,或者是否会继续到程序的下一部分完全授权。在那里添加一些打印语句只是为了确保。但我很确定这应该是它的工作方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-30
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 2020-01-22
    相关资源
    最近更新 更多