Search code examples
javascriptfirebasereact-nativefirebase-authenticationreact-native-firebase

I get error when using Firebase phone authentication on React Native?


when trying to register with phone number by Firebase phone authentication and using my own number i get this error:
ERROR [TypeError: undefined is not an object (evaluating 'verifier.verify')]

here are the necessary codes just just know i used my phone number instead "test number"

import {
  getAuth,
  createUserWithEmailAndPassword,
  verifyPhoneNumber,
  PhoneAuthProvider,
  signInWithCredential,
  onAuthStateChanged,
} from "firebase/auth";
///////////////////////////////////////////////////////
  const auth = getAuth();


  async function handleVerifyPhoneNumber(phoneNumber) {
    try {
      const provider = new PhoneAuthProvider(auth);
      const confirmation = await provider.verifyPhoneNumber(phoneNumber);
      setConfirm(confirmation);
    } catch (error) {
      console.error(error);
    }
  }
///////////////////////////////////////////////////////
            <Button
              title="Verify Phone Number"
              onPress={() => handleVerifyPhoneNumber("test Number")}
            />
        

Solution

  • this is the way to works with my code:

     const auth = getAuth(app);
          const appVerifier = recaptchaVerifier.current;
          const fullPhoneNumber = `+${countryCallingCode}${phoneNumber.replace(
            /^0+/,
            ""
          )}`;
    
          const confirmResult = await signInWithPhoneNumber(
            auth, 
            fullPhoneNumber,
    
            appVerifier
          );