0
votes

i'm using react native to build an authentication app but i cant past the this error

how to get rid of this error?

Error: No Firebase App '[DEFAULT]' has been created - call firebase.initializeApp()

I was following the directions from reactnative firebase docs

https://rnfirebase.io/auth/usage

here is my app.js

import React, {useState, useEffect }from 'react';
import {StyleSheet,View,Text,} from 'react-native';
import auth from '@react-native-firebase/auth';
import firebase from 'firebase';
import 'firebase/firestore';


const firebaseConfig = {
  apiKey: "A********",
  authDomain: "luxuryvipservices-90825.firebaseapp.com",
  databaseURL: "https://luxuryvipservices-90825.firebaseio.com",
  projectId: "luxuryvipservices-90825",
  storageBucket: "luxuryvipservices-90825.appspot.com",
  messagingSenderId: "726338383371",
  appId: "1:726338383371:web:6ef2b42733765d6771610e",
  measurementId: "G-QVZRY63LX1"
};
firebase.initializeApp(firebaseConfig);

const App = () => {

  const [initializing, setInitializing] =useState(true);
  const [user, setUser] = useState()

  function onAuthStateChanged(user){
    setUser(user);
    if (initializing) setInitializing(false)
  }

  useEffect(()=> {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
    return subscriber;
  }, []);

  if (initializing) return null;
  if (!user){
    return(
      <View>
        <Text>Login</Text>
      </View>
    )
  }
  return (
    <>
      <View>
  <Text>Welcome {user.email}</Text>
      </View>
    </>
  );
};

const styles = StyleSheet.create({
  
});

export default App;
1
You are somehow calling initializeApp more than once. You're going to have to figure out how that happened. - Doug Stevenson
yes thats why I posted the question on stackoverflow because i couldnt figure it out - c.anaba

1 Answers

0
votes

Try this:

import React, {useState, useEffect }from 'react';
import {StyleSheet,View,Text,} from 'react-native';
import auth from '@react-native-firebase/auth';
import firebase from 'firebase';
import 'firebase/firestore';


const firebaseConfig = {
  apiKey: "A********",
  authDomain: "luxuryvipservices-90825.firebaseapp.com",
  databaseURL: "https://luxuryvipservices-90825.firebaseio.com",
  projectId: "luxuryvipservices-90825",
  storageBucket: "luxuryvipservices-90825.appspot.com",
  messagingSenderId: "726338383371",
  appId: "1:726338383371:web:6ef2b42733765d6771610e",
  measurementId: "G-QVZRY63LX1"
};

const App = () => {

  const [initializing, setInitializing] =useState(true);
  const [user, setUser] = useState()

  firebase.initializeApp(firebaseConfig);
  
  function onAuthStateChanged(user){
    setUser(user);
    if (initializing) setInitializing(false)
  }

  useEffect(()=> {
    const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
    return subscriber;
  }, []);

  if (initializing) return null;
  if (!user){
    return(
      <View>
        <Text>Login</Text>
      </View>
    )
  }
  return (
    <>
      <View>
  <Text>Welcome {user.email}</Text>
      </View>
    </>
  );
};

const styles = StyleSheet.create({
  
});

export default App;

It may work but ideally you should download Google Services file and place it inside your project. Just follow the tutorial on firebase website;