Leon Bogod
Leon Bogod

Reputation: 428

how to intiialize two firebase apps

im trying to initialize two firebase apps but getting an annoying error Firebase: Firebase App named '[DEFAULT]' already exists (app/duplicate-app). Im not sure how to fix this and how to make two seperate firebase. Any help is greatly appreciated!

database instance

import * as firebase from "firebase";

 var config = {
    apiKey: "AIzaSyC....",
    authDomain: "car-app-506db.firebaseapp.com",
    databaseURL: "https://car-app-506db.firebaseio.com",
    projectId: "car-app-506db",
    storageBucket: "car-app-506db.appspot.com",
    messagingSenderId: "496521253390"
  };
  firebase.initializeApp(config);


const databaseRef = firebase.database().ref();
export const CarsRef = databaseRef.child("Cars");
export const authRef = firebase.auth();
export const provider = new firebase.auth.GoogleAuthProvider();

export const timeRef = firebase.database.ServerValue.TIMESTAMP;

export default databaseRef;

auth instance

import firebase from "firebase";
import 'firebase/auth';
import 'firebase/database';

var firebaseConfig = {
    apiKey: "AIzaSyC6C-8A-vfsEzv31acwjhru8N64g0Hzhcc",
    authDomain: "car-app-506db.firebaseapp.com",
    databaseURL: "https://car-app-506db.firebaseio.com",
    projectId: "car-app-506db",
    storageBucket: "car-app-506db.appspot.com",
    messagingSenderId: "496521253390"
};



var app = firebase.initializeApp(app);
export default app;

Upvotes: 1

Views: 1081

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598797

Each FirebaseApp instance has its own configuration and its own unique name. If you don't specify a name when you initialize the app, Firebase assumes your (re)initializing the default app. Since neither if your calls to firebase.initializeApp specifies a name, you're initializing the same app twice, which is an error.

The solution is to name your apps by passing a name (string) in to the second call to initializeApp. For example:

var firebaseConfig = {
    apiKey: "AIzaSyC6C-8A-vfsEzv31acwjhru8N64g0Hzhcc",
    authDomain: "car-app-506db.firebaseapp.com",
    databaseURL: "https://car-app-506db.firebaseio.com",
    projectId: "car-app-506db",
    storageBucket: "car-app-506db.appspot.com",
    messagingSenderId: "496521253390"
};



var app = firebase.initializeApp(firebaseConfig, "auth");
export default app;

Also see using multiple projects in your application in the Firebase docs.

Upvotes: 5

Related Questions