firebase-js-sdk icon indicating copy to clipboard operation
firebase-js-sdk copied to clipboard

`signInWithPhoneNumber()`error - "TypeError: Cannot read properties of undefined (reading 'settings')"

Open russellwheatley opened this issue 3 years ago • 4 comments

[REQUIRED] Describe your environment

  • Operating System version: macOS 12.4
  • Browser version: Chrome 103.0.5060.114 (latest)
  • Firebase SDK version: 9.9.0
  • Firebase Product: auth

[REQUIRED] Describe the problem

Encountering error when trying to use signInWithPhoneNumber().

TypeError: Cannot read properties of undefined (reading 'settings')

Screenshot of where in source code: Screenshot 2022-07-19 at 09 08 18

Steps to reproduce:

Run the following code. I reproduced using rollup.js as the bundler:

import { initializeApp } from 'firebase/app';

import { signInWithPhoneNumber,  initializeAuth } from 'firebase/auth';

var config = {
 // INSERT YOUR OWN FIREBASE CONFIG
}

const app = initializeApp(config);


const auth = initializeAuth(app);

// set "appVerificationDisabledForTesting"
auth.settings.appVerificationDisabledForTesting = true;

function testRecaptcha(){
  const id = 'ff-container';
  const element = document.createElement("div");
  element.setAttribute("id", id);
  document.body.appendChild(element);

  const recaptchaVerifier = new RecaptchaVerifier(id, {
    size: 'normal',
    theme:'light',
    callback: function(response) {
      console.log('Response', response);
    },
    'error-callback': function(error) {
      console.log('Error', error);
    },
    'expired-callback': function() {
      console.log('Expired');
    }
  });
  
  signInWithPhoneNumber(auth, '+111222444', recaptchaVerifier).then((response)=>{
    console.log('Response', response);
  }).catch((e)=> {  console.log('Error ', e); });

}

testRecaptcha();

This is also an issue on FlutterFire which I help to maintain: https://github.com/firebase/flutterfire/issues/9097

russellwheatley avatar Jul 19 '22 08:07 russellwheatley

It looks like in this code, only 2 params are being passed to the RecaptchaVerifier constructor, and it requires 3, the third one being the auth instance: https://firebase.google.com/docs/reference/js/auth.recaptchaverifier

hsubox76 avatar Jul 19 '22 16:07 hsubox76

Thanks! That was it. You may want to update the documentation here as it says:

"If none is provided, the default Firebase Auth instance is used. A Firebase Auth instance must be initialized with an API key, otherwise an error will be thrown."

When it appears it will error when auth instance is not provided.

russellwheatley avatar Jul 20 '22 08:07 russellwheatley

@sam-gc Was it intended for the RecaptchaVerifier constructor to supply the default auth instance if no 3rd argument is provided? I don't see a default getAuth() anywhere else in the auth code so I'm guessing not. I can update the comment if that's the case.

hsubox76 avatar Jul 20 '22 16:07 hsubox76

Ah yeah good catch, it's always expected to pass the auth instance. I just sent out https://github.com/firebase/firebase-js-sdk/pull/6460

sam-gc avatar Jul 20 '22 23:07 sam-gc

I'm closing this issue for now since it seems like the docs have been updated. Please @ message me if that's incorrect, or open a new issue that refers to this one. Thanks!

DellaBitta avatar May 29 '23 00:05 DellaBitta