3

I am trying to implement firebase phone authentication in react js without using the firebase UI. How do I do it?

code

requestVerificationCode = () => {
    const { phoneNumber } = this.state;
    const appVerifier = new firebase.auth.RecaptchaVerifier(
      "recaptcha-container"
    );
    if (phoneNumber < 10) {
      this.setState({ error: true });
    } else {
      this.setState({ message: "Sending code ..." });

      firebase
        .auth()
        .signInWithPhoneNumber(phoneNumber, appVerifier)
        .then(confirmResult =>
          this.setState({ confirmResult, verifying: true })
        )
        .catch(error =>
          this.setState({
            message: `Sign In With Phone Number Error: ${error.message}`
          })
        );
    }
  };

error


auth.esm.js:282 Uncaught K {code: "auth/argument-error", message: "reCAPTCHA container is either not found or already contains inner elements!"}

5
  • 1
    what have you tried so far? Commented Jul 25, 2018 at 7:13
  • what was the error? Commented Jul 25, 2018 at 7:40
  • 1
    You need to have an element with the ID of recaptcha-container in your html Commented Jul 25, 2018 at 16:08
  • Still not working @TheUnreal Commented Jul 25, 2018 at 21:16
  • I don't have enough reputation to comment, but I managed to get the above working by including e.preventDefault(). Commented Aug 31, 2019 at 3:35

2 Answers 2

5

The important part is to wait for componentDidMount() or mounted() in Vue JS so that the dom element containing "recaptcha-container" is mounted.

HTML

<input id="recaptcha-container" type="button" onClick="this.onClick" />

JS

componentDidMount () {
    window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier("recaptcha-container",
    {
       size:"invisible"
        // other options
    });
}

onClick() {
    const phoneNumber = this.phone;
    const appVerifier = window.recaptchaVerifier;
    firebase
    .auth()
    .signInWithPhoneNumber(phoneNumber, appVerifier)
    .then(confirmResult => {
      // success
    })
    .catch(error => {
      // error
    });
}

If you re-direct the user away from your component where id="recaptcha-container" lives, then recaptcha will work fine but throw a style related error in the console, but that's because it's wants a permanent place on the page.

Sign up to request clarification or add additional context in comments.

Comments

0

In React JS you will need to put this in index.html in public folder.

<input id="recaptcha-container" type="button" onClick="this.onClick" />

Alternatively, you can also create the html on dom.

  const tag = document.createElement("input");
  tag.id = "recaptcha-container"; // need to be same id as your firebase.auth.RecaptchaVerifier below
  tag.type="button" 
  tag.onClick="this.onClick"
  document.body.appendChild(tag);

then

window.recaptchaVerifier = new firebase.auth.RecaptchaVerifier("recaptcha-container", {
    size: "invisible",
    callback: (response) => {
      // reCAPTCHA solved, allow signInWithPhoneNumber.
      console.log("Submitted  window.recaptchaVerifier: ", response);

    },
    "expired-callback": (e) => {
      console.log("Expired Callback: ", e);
      // Response expired. Ask user to solve reCAPTCHA again.
      // ...
    },
  });

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.