Firebase.app() error when i try to implement auth

Viewed 1476

i'm trying to connect the DB to my App.js, it works, but when i try to do the auth, this error come out:

Error: You attempted to use a firebase module that's not installed on your Android project by calling firebase.app(). Ensure you have:

  1. imported the 'io.invertase.firebase.app.ReactNativeFirebaseAppPackage' module in your 'MainApplication.java' file.
  2. Added the 'new ReactNativeFirebaseAppPackage()' line inside of the RN 'getPackages()' method list.

This is my App.js before auth (working)

import * as firebase from 'firebase';
import 'firebase/firestore';

if (firebase.apps.length === 0){
  firebase.initializeApp(firebaseConfig);
}

export default class App extends React.Component {
  render() {
    return (
      <NavigationContainer>
        <RestaurantNavigator />
      </NavigationContainer>
    )
  }
}

This is my App.js after auth (notWorking, see error of before)

import * as firebase from 'firebase';
import 'firebase/firestore';
import auth from '@react-native-firebase/auth';

if (firebase.apps.length === 0){
  firebase.initializeApp(firebaseConfig);
}

function LoginApp(){
  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; // unsubscribe on unmount
  }, []);

  if (initializing) return null;

  if (!user) {
    return (
      <View>
        <Text>Login</Text>
      </View>
    );
  }
}

export default class App extends Component {
  render() {
    return (
      <View style = {styles.container}>
        <LoginApp />
        <NavigationContainer>
          <RestaurantNavigator />
        </NavigationContainer>
      </View>
    )
  }   
}

What i'm doing wrong?

1 Answers

Add In main application.java file "import io.invertase.firebase.auth.RNFirebaseAuthPackage;" and in package "packages.add(new RNFirebaseAuthPackage());"

enter image description here

And in android>app>build.gradle file in dependencies add "implementation 'com.google.firebase:firebase-auth:19.4.0' "

enter image description here

Related