9

I am trying to create a React application that uses an "onSubmit" button to trigger handleSignUp(). However, each time I call handleSignUp() it gives this error

TypeError: _base__WEBPACK_IMPORTED_MODULE_2__.default.auth.RecaptchaVerifier is not a constructor

firebase initialize -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;

SignUp.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);

and also run var appVerifier = new app.auth.RecaptchaVerifier("recaptcha-container"); automatically refresh the web page

1
  • 1
    can you post the solution? Commented Jul 14, 2020 at 19:55

3 Answers 3

2

I spent 3hrs on this and finally found the issue. When you initialize firebase app like

app.initializeApp(config);

then you can call methods using: app.auth()

app.auth().createUserWithEmailAndPassword(email, password);

and to create RecaptchaVerifier instance, you must use: app.auth

window.recaptchaVerifier = new app.auth.RecaptchaVerifier('captcha-container', {
        'size': 'invisible'
    });
Sign up to request clarification or add additional context in comments.

1 Comment

Tested it, and it isn't working because the "app"-Object has no Field "auth". You pass the auth object from the result of getAuth(app) as the first Parameter.
2

I'm also face this issue and resolved. This code absolutely help to signIn using firebase phone auth

Firebase.js

import * as Firebase from 'firebase';

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
});
Firebase.initializeApp(app);
export default Firebase;

signup.js

    import React, { useState  } from 'react';
    import Firebase from './container/Firebase';
    import 'react-phone-number-input/style.css'
    import PhoneInput from 'react-phone-number-input'

    const Signup= () => {

    const [value, setValue] = useState(0);

function setuprecaptcha (){
    window.recaptchaVerifier = new Firebase.auth.RecaptchaVerifier('recaptcha-container', {
        size: 'invisible',
        callback: function (response) {
            console.log("recature resolved")
            this.onSignInSubmit();
        }
    });

}

 
function onSignInSubmit(event) {
    
    event.preventDefault();
    setuprecaptcha();
    var phoneNumber = value;
    var appVerifier = window.recaptchaVerifier;
    Firebase.auth().signInWithPhoneNumber(phoneNumber, appVerifier)
        .then(function (confirmationResult) {
            console.log("Success");
            // SMS sent. Prompt user to type the code from the message, then sign the
            // user in with confirmationResult.confirm(code).
            window.confirmationResult = confirmationResult;
            var verificationId = window.prompt("Enter otp")
            confirmationResult
                .confirm(verificationId)
                .then(function (result) {
                    // User signed in successfully.
                    var user = result.user;
                    user.getIdToken().then(idToken => {
                        window.localStorage.setItem('idToken', idToken);

                       
                        console.log(idToken);
                    });
                })
                .catch(function (error) {
                    // User couldn't sign in (bad verification code?)
                    console.error("Error while checking the verification code", error);
                    window.alert(
                        "Error while checking the verification code:\n\n" +
                        error.code +
                        "\n\n" +
                        error.message
                    );
                });

        })
        .catch(function (error) {
            console.log("sign Up error:" + error.code);
        });

}

Comments

0

RecaptchaVerifier constructor resides in firebase.auth. You need to pass window.recaptchaVerifier as the second argument to signInWithPhoneNumber.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.