无法从生产中的Firebase接收电话身份验证短信

问题描述

我正在使用Firebase电话身份验证通过我的React + Node应用程序中的手机号码登录用户。在localhost上一切正常,正在向短信发送验证码,并且用户可以登录

但是当我部署该应用程序时,它不再起作用。输入手机号码并完成验证码后,它只会重新加载验证码,我必须再次完成验证,并且它永远不会发送短信。

我的控制台中出现此错误

POST https://www.googleapis.com/identitytoolkit/v3/relyingparty/sendVerificationCode?key=myFirebaseAPIkey 400

这仅在生产中发生。我不确定为什么会这样。我的主机会阻止某些端口Firebase发送短信吗?否则我出事了。

这是我的登录方法

loginWithPhone = () => {
this.setState({ showOnlyCaptcha: true },() => {
  window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier(
    "recaptcha-container"
  );

  var phoneNumber = "+91" + this.state.loginWithMobile;
  var appVerifier = window.recaptchaVerifier;
  firebase
    .auth()
    .signInWithPhoneNumber(phoneNumber,appVerifier)
    .then((confirmationResult) => {
      // SMS sent. Prompt user to type the code from the message,then sign the
      // user in with confirmationResult.confirm(code).
      window.confirmationResult = confirmationResult;
    })
    .catch(function (error) {
      // Error; SMS not sent
      if (error.code === "auth/too-many-requests") {
      this.setState(
        {
          loginWithPhoneErrorMessage:
            "Unable to verify Captcha at this time. Please try again after some time.",},() => {
          this.setState({ loginWithPhoneError: true });
        }
      );
      }
    });
});

};

解决方法

经过一些研究,我发现了这一点。 为了能够在生产中使用auth,您需要将您的域添加到Firebase项目的“授权域”列表中。之后,所有登录方法都将在您的域中起作用。