I am trying to use firebase phone auth in my react native application, I have copied the code from the expo documentation as it is, at the first time it worked with recaptcha feature and otp message sending. but when I tried a second time it shows me the error :
[Error: Either an instance of firebase.auth.Auth must be passed as an argument to the firebase.auth.PhoneAuthProvider constructor, or the default firebase App instance must be initialized via firebase.initializeApp().]
My firebase app is already initialized and there is no passing arguments to the specified method in the error from the expo documentation. So I cannot really follow the origin of the error.
Here is the code that I have used from the documentation:
import * as React from "react";
import {
Text,
View,
TextInput,
Button,
StyleSheet,
TouchableOpacity,
Platform,
} from "react-native";
import { FirebaseRecaptchaVerifierModal } from "expo-firebase-recaptcha";
import * as firebase from "firebase";
import firebaseConfig from "../Config/firebaseConfig";
// Initialize Firebase JS SDK
// https://firebase.google.com/docs/web/setup
try {
firebase.initializeApp({
firebaseConfig,
});
} catch (err) {
// ignore app already initialized error in snacé
}
export default function App() {
const recaptchaVerifier = React.useRef(null);
const [phoneNumber, setPhoneNumber] = React.useState();
const [verificationId, setVerificationId] = React.useState();
const [verificationCode, setVerificationCode] = React.useState();
const firebaseConfig = firebase.apps.length
? firebase.app().options
: undefined;
const [message, showMessage] = React.useState(
!firebaseConfig || Platform.OS === "web"
? {
text:
"To get started, provide a valid firebase config in App.js and open this snack on an iOS or Android device.",
}
: undefined
);
return (
<View style={{ padding: 20, marginTop: 50 }}>
<FirebaseRecaptchaVerifierModal
ref={recaptchaVerifier}
firebaseConfig={firebaseConfig}
/>
<Text style={{ marginTop: 20 }}>Enter phone number</Text>
<TextInput
style={{ marginVertical: 10, fontSize: 17 }}
placeholder="+1 999 999 9999"
autoFocus
autoCompleteType="tel"
keyboardType="phone-pad"
textContentType="telephoneNumber"
onChangeText={(phoneNumber) => setPhoneNumber(phoneNumber)}
/>
<Button
title="Send Verification Code"
disabled={!phoneNumber}
onPress={async () => {
// The FirebaseRecaptchaVerifierModal ref implements the
// FirebaseAuthApplicationVerifier interface and can be
// passed directly to `verifyPhoneNumber`.
try {
const phoneProvider = new firebase.auth.PhoneAuthProvider();
const verificationId = await phoneProvider.verifyPhoneNumber(
phoneNumber,
recaptchaVerifier.current
);
setVerificationId(verificationId);
showMessage({
text: "Verification code has been sent to your phone.",
});
} catch (err) {
showMessage({ text: `Error: ${err.message}`, color: "red" });
}
}}
/>
<Text style={{ marginTop: 20 }}>Enter Verification code</Text>
<TextInput
style={{ marginVertical: 10, fontSize: 17 }}
editable={!!verificationId}
placeholder="123456"
onChangeText={setVerificationCode}
/>
<Button
title="Confirm Verification Code"
disabled={!verificationId}
onPress={async () => {
try {
const credential = firebase.auth.PhoneAuthProvider.credential(
verificationId,
verificationCode
);
await firebase.auth().signInWithCredential(credential);
showMessage({ text: "Phone authentication successful ????" });
} catch (err) {
showMessage({ text: `Error: ${err.message}`, color: "red" });
}
}}
/>
{message ? (
<TouchableOpacity
style={[
StyleSheet.absoluteFill,
{ backgroundColor: 0xffffffee, justifyContent: "center" },
]}
onPress={() => showMessage(undefined)}
>
<Text
style={{
color: message.color || "blue",
fontSize: 17,
textAlign: "center",
margin: 20,
}}
>
{message.text}
</Text>
</TouchableOpacity>
) : undefined}
</View>
);
}
Any help with this error please? Thank you.