2
votes

I am getting this error in my react-native expo project: FirebaseError: Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app). I have cross checked my project and I have not imported firebase anywhere else in the project. I have also made a firebase.js file and initialised the firebase config there. Here is my code:

This is my firebase.js file

import firebase from "firebase";
const firebaseConfig = {
  apiKey: "XXXXXXXXXXXX",
  authDomain: "XXXXXXXXXXXX",
  databaseURL: "XXXXXXXXXXXX",
  projectId: "XXXXXXXXXXXX",
  storageBucket: "XXXXXXXXXXXX",
  messagingSenderId: "XXXXXXXXXXXX",
  appId: "XXXXXXXXXXXX",
};
const Firebase = firebase.initializeApp(firebaseConfig);
export default Firebase;

This is the Signin.js file where I want to use firebase.

import { StatusBar } from "expo-status-bar";
import React, { useState } from "react";
import {
  StyleSheet,
  Text,
  View,
  TextInput,
  TouchableOpacity,
  Picker,
  ScrollView,
} from "react-native";
import { FontAwesome } from "@expo/vector-icons";
import { LinearGradient } from "expo-linear-gradient";
import firebase from "./firebase";
import "firebase/auth";

function Signin() {
  firebase
    .auth()
    .signInWithEmailAndPassword("[email protected]", "bhavansh")
    .catch(function (error) {});
}
export default function LogIn({ navigation }) {
  const [selectedValue, setSelectedValue] = useState("java");

  return (
    <View style={styles.container}>
      <View style={styles.header}>
        <Text style={styles.header_text}>Register</Text>
      </View>
      <View style={styles.footer}>
        <ScrollView
          showsVerticalScrollIndicator={false}
          style={styles.ScrollView}
          show
        >
          <Text style={styles.fields}>Name</Text>

          <TextInput
            placeholder="Enter your Name"
            style={styles.input}
          ></TextInput>
          <Text style={styles.fields}>Email</Text>

          <TextInput placeholder="Enter Email" style={styles.input}></TextInput>

          <Text style={styles.fields}>Password</Text>

          <TextInput placeholder="Enter Password" style={styles.input} />
          <Text style={styles.fields}>Confirm Password</Text>

          <TextInput
            placeholder="Enter Confirm Password"
            style={styles.input}
          />
          <View style={styles.picker}>
            <TouchableOpacity style={styles.position}>
              <LinearGradient
                colors={["#08d4c4", "#01ab9d"]}
                style={styles.picker_gradient}
              >
                <Text style={styles.button_text}>Passenger</Text>
              </LinearGradient>
            </TouchableOpacity>
            <TouchableOpacity
              style={styles.position}
              onPress={() => navigation.navigate("SignIn")}
            >
              <Text style={styles.sign_button_text}>Rider</Text>
            </TouchableOpacity>
          </View>

          <TouchableOpacity style={styles.button} onPress={() => Signin()}>
            <LinearGradient
              colors={["#08d4c4", "#01ab9d"]}
              style={styles.gradient_style}
            >
              <Text style={styles.button_text}>Register</Text>
            </LinearGradient>
          </TouchableOpacity>
          <TouchableOpacity
            style={styles.sign_button}
            onPress={() => navigation.navigate("LogIn")}
          >
            <Text style={styles.sign_button_text}>Log In</Text>
          </TouchableOpacity>
        </ScrollView>
      </View>
    </View>
  );
}
2
At what point do you get the error? - Jay Codist

2 Answers

5
votes

I'm assuming you are getting the error when you do hot reload or etc... Anyways the problem is, Firebase tries to create new instance with the exact same config which it can not. So it throws an error about it. You can overcome this error by simple if check:

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

Erkin Kurt is right and keep in mind that Firebase 8 introduced some breaking changes: https://firebase.google.com/support/release-notes/js#version_800_-_october_26_2020

Now you can make it work like this if you use .default:

const firebase = require('firebase/app').default

if (!firebase.apps.length) {
    firebase.initializeApp(firebaseConfig)
}