如何使Firebase OTP返回SMS代码,以便将其用于验证以外的其他验证?

发布于 2025-01-19 12:45:32 字数 2328 浏览 0 评论 0原文

以下Firebase功能将OTP代码发送到电话号码。 问题是.. _onverificationcompleted仅在发送请求的设备上的电话号码(SIM卡)时才调用。 如果我需要知道_onverificationcompleted方法将永远不会被调用时,我需要知道已发送的OTP代码怎么办? 我尝试了phoneAuthProvider.credential(verificationId:验证:smscode:enteredCode);> 但是,此方法的返回类型是PhoneAuthcredential,仅在尝试签名/登录用户时才使用。

我只需要验证输入的代码== smscode而不签名即可。 任何帮助都将不胜感激!

Future<void> verifyPhoneNumber({@required String phoneNumber}) async {
    await _auth.verifyPhoneNumber(
        phoneNumber: phoneNumber,
        verificationCompleted: _onVerificationCompleted,
        verificationFailed: _onVerificationFailed,
        codeSent: _onCodeSent,
        codeAutoRetrievalTimeout: _onCodeTimeout);
  }

  _onVerificationCompleted(PhoneAuthCredential authCredential) async {
    print("verification completed ${authCredential.smsCode}");
    if (otpTextFieldController.text == authCredential.smsCode) {
      print('Verified!');
    } else {
      print('not-verified!');
    }
    setState(() {
      _numberValidated = true;
    });
    if (authCredential.smsCode != null) {
      try {} on FirebaseAuthException catch (e) {
        print('OTP error : $e');
      }
      setState(() {
        isLoading = false;
      });
    }
  }

  _onVerificationFailed(FirebaseAuthException exception) {
    print('verification : ${exception}');
    if (exception.code == 'invalid-phone-number') {
      showMessage("The phone number entered is invalid!");
    }
  }

  _onCodeSent(String verificationId, int forceResendingToken) {
    this.verificationId = verificationId;
    print(forceResendingToken);
    print("code sent = ${this.verificationId}");
  }

  _onCodeTimeout(String timeout) {
    return null;
  }

  void showMessage(String errorMessage) {
    showDialog(
        context: context,
        builder: (BuildContext builderContext) {
          return AlertDialog(
            title: Text("Error"),
            content: Text(errorMessage),
            actions: [
              TextButton(
                child: Text("Ok"),
                onPressed: () async {
                  Navigator.of(builderContext).pop();
                },
              )
            ],
          );
        }).then((value) {
      setState(() {
        isLoading = false;
      });
    });
  }

The following firebase function sends the OTP code to the phone number.
Problem is.. _onVerificationCompleted gets only invoked when the phone number is on the device that is sending the request (SIM card).
What if I need to know the sent otp code when the _onVerificationCompleted method will never get called as it's a different device phone number?
I tried PhoneAuthProvider.credential(verificationId: verification, smsCode: enteredCode);
But this method's return type is PhoneAuthCredential, which is only used when trying to signin/login the user.

I just need to verify the entered code == the smsCode without signing in.
Any help would be really appreciated!

Future<void> verifyPhoneNumber({@required String phoneNumber}) async {
    await _auth.verifyPhoneNumber(
        phoneNumber: phoneNumber,
        verificationCompleted: _onVerificationCompleted,
        verificationFailed: _onVerificationFailed,
        codeSent: _onCodeSent,
        codeAutoRetrievalTimeout: _onCodeTimeout);
  }

  _onVerificationCompleted(PhoneAuthCredential authCredential) async {
    print("verification completed ${authCredential.smsCode}");
    if (otpTextFieldController.text == authCredential.smsCode) {
      print('Verified!');
    } else {
      print('not-verified!');
    }
    setState(() {
      _numberValidated = true;
    });
    if (authCredential.smsCode != null) {
      try {} on FirebaseAuthException catch (e) {
        print('OTP error : $e');
      }
      setState(() {
        isLoading = false;
      });
    }
  }

  _onVerificationFailed(FirebaseAuthException exception) {
    print('verification : ${exception}');
    if (exception.code == 'invalid-phone-number') {
      showMessage("The phone number entered is invalid!");
    }
  }

  _onCodeSent(String verificationId, int forceResendingToken) {
    this.verificationId = verificationId;
    print(forceResendingToken);
    print("code sent = ${this.verificationId}");
  }

  _onCodeTimeout(String timeout) {
    return null;
  }

  void showMessage(String errorMessage) {
    showDialog(
        context: context,
        builder: (BuildContext builderContext) {
          return AlertDialog(
            title: Text("Error"),
            content: Text(errorMessage),
            actions: [
              TextButton(
                child: Text("Ok"),
                onPressed: () async {
                  Navigator.of(builderContext).pop();
                },
              )
            ],
          );
        }).then((value) {
      setState(() {
        isLoading = false;
      });
    });
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文