React - Firebase - RecaptchaVerifier 不是构造函数

Mit*_*nda 5 javascript firebase reactjs firebase-authentication

我正在尝试创建一个 React 应用程序,该应用程序使用“onSubmit”按钮触发handleSignUp(). 但是,每次我调用handleSignUp()它时都会出现此错误

类型错误:_base__WEBPACK_IMPORTED_MODULE_2__.default.auth.RecaptchaVerifier 不是构造函数

firebase 初始化 -base.js

import * as firebase from "firebase/app";
import "firebase/auth";

const app = firebase.initializeApp({
  apiKey: process.env.REACT_APP_FIREBASE_KEY,
  authDomain: process.env.REACT_APP_FIREBASE_DOMAIN,
  databaseURL: process.env.REACT_APP_FIREBASE_DATABASE,
  projectId: process.env.REACT_APP_FIREBASE_PROJECT_ID,
  storageBucket: process.env.REACT_APP_FIREBASE_STORAGE_BUCKET,
  messagingSenderId: process.env.REACT_APP_FIREBASE_SENDER_ID
});

export default app;
Run Code Online (Sandbox Code Playgroud)

注册.js

import React, { Component } from "react";
import { withRouter } from "react-router";
import app from "./base";

class SignUp extends Component {
  componentDidMount() {
    window.recaptchaVerifier = new app.auth.RecaptchaVerifier(this.recaptcha, {
      size: "normal",
      callback: function(response) {
        console.log("reCAPTCHA solved, allow signInWithPhoneNumber.");
      },
      "expired-callback": function() {
        console.log("Response expired. Ask user to solve reCAPTCHA again.");
      }
    });
    window.recaptchaVerifier.render().then(function(widgetId) {
      window.recaptchaWidgetId = widgetId;
    });
  }

  handleSignUp = async () => {
    var phoneNumber = "+16505554567";
    var appVerifier = new app.auth.RecaptchaVerifier("recaptcha-container");

    try {
      await app
        .auth()
        .signInWithPhoneNumber(phoneNumber, appVerifier)
        .then(function(confirmationResult) {
          window.confirmationResult = confirmationResult;
        });
    } catch (error) {
      alert(error);
    }
  };
  render() {
    return (
      <div ref={ref => (this.recaptcha = ref)}>
        <h1>Sign up</h1>
        <form onSubmit={this.handleSignUp}>
          <button type="submit">Sign Up</button>
        </form>
      </div>
    );
  }
}

export default withRouter(SignUp);
Run Code Online (Sandbox Code Playgroud)

并运行 var appVerifier = new app.auth.RecaptchaVerifier("recaptcha-container");自动刷新网页

小智 0

RecaptchaVerifier构造函数驻留在firebase.auth. 您需要window.recaptchaVerifier作为第二个参数传递给signInWithPhoneNumber.