firebase / firebase-js-sdk

Firebase Javascript SDK
https://firebase.google.com/docs/web/setup
Other
4.82k stars 884 forks source link

Auth error only after recaptcha: "Cannot read property 'style' of null" #3356

Open zachasme opened 4 years ago

zachasme commented 4 years ago

Describe your environment

Describe the problem

I am setting up Phone Auth with firebase. If there is a problem with the phone number (too long, too short, etc), but the captcha is shown (delaying the error until after resolving the captcha), the following error is thrown:

Uncaught TypeError: Cannot read property 'style' of null

I have a feeling it happens when firebase auth attemps to reset the recaptcha after a problem with phone auth occurs.

There is no issue when captcha is shown but phone number is valid.

Steps to reproduce:

Uncaught TypeError: can't access property "style", r is null
    B https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:310
    Y https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:120
    setTimeout handler*n</< https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:132
    L https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:120
    L https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:123
    B https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:300
    <anonymous> https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:654
    T https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:125
    T https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:125
    B https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:351
    next https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:107
    u https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:266
    promise callback*C https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:266
    N https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:266
    f https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:266
    f https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:218
    L https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:125
    JA https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:369
    G https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:53
    vE https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:449
    E https://www.gstatic.com/recaptcha/releases/NMoy4HgGiLr5NAQaEQa2ho8X/recaptcha__en.js:26
recaptcha__en.js:310:363

Relevant Code:

https://codesandbox.io/s/compassionate-bouman-vw5zn?file=/index.html

Related issues:

Anas-7 commented 6 months ago

I have the exact same issue as @andresemartinez where the image selector is causing the issue in the recaptcha_en.js file. I face this issue even on normal selectors, and as mentioned when invalid-phone-number error occurs. In my case, I face it even if no error occurs in Firebase sometimes making the entire feature quite unreliable

Anas-7 commented 6 months ago

Hi all, I was recently working on this problem and might have at least a temporary solution. I suggest calling the reset function in each type of callback of the recaptchaVerifier. I have done this with Firebase version 10.8.0

Here is a code snippet of the same:

if (!window.recaptchaVerifier) {
      const button = document.getElementById('recaptcha-container');
      if (button !== undefined && button !== null) {
        const verifier = new RecaptchaVerifier(auth, 'recaptcha-container', {
          size: 'invisible',
          /*
          Motivation for the code:
          1. https://github.com/firebase/firebase-js-sdk/issues/3356
          2. https://github.com/jsardev/reaptcha/issues/218
          3. https://github.com/dozoisch/react-google-recaptcha/issues/129

          Cannot read property 'style' of null was the error message, and to get/test this error do the following steps:
          1. Enter a number, and an invalid number ideally
          2. Keep trying to submit till you see the captcha of image selector where it asks you to select buses, etc
          3. Once you click on Verify, it will show an error screen, and in some cases even when number was valid.

          Since the error was not due to any other code in this file, I decided to reset the captcha in each type of callback, and this seems to have solved the error
          */
          callback: () => {
            window.recaptchaVerifier.recaptcha.reset();
          },
          'expired-callback': () => {
            window.recaptchaVerifier.recaptcha.reset();
          },
          'error-callback': () => {
            window.recaptchaVerifier.recaptcha.reset();
          },
        });
        window.recaptchaVerifier = verifier;
      }
   }
philly-vanilly commented 5 months ago

HEY GUYS I FOUND THE SOLUTION ...APPARENTLY I WAS USING TEST PHONE NUMBERS BUT ONCE I SIGN IN WITH IT I NEED TO DELETE THAT ACCOUNT FROM FIREBASE USERS SECTION TO BE ABLE TO LOGIN AGAIN WITH THAT TEST NUMBER OTHERWISE RECAPTCHA THROWS THIS ERROR...this worked for me i hope it helps

why are you shouting