Search code examples
javascriptfirebasefirebase-authenticationfirebaseui

Firebase login with multiple app instances issue


I have two Firebase app instances, when i login, the first (default) instance is populated with the current user, but the secondary app doesn't.

firebase.auth().currentUser;//this returns the currently logged in user
firebase.auth(secondaryFirebaseApp).currentUser;//this returns null

This is how i'm initializing the secondary Firebase app:

var config = {
        databaseURL: "XXXXXXXXXXXXXX",
        apiKey: "XXXXXXXXXXXXXXXXXX",
        authDomain: "XXXXXXXXXXXXXXXXXXXXXX",
        projectId: "XXXXXXXXXXXXXXXXXXXX",
    };
secondaryFirebaseApp= firebase.initializeApp(config, "secondaryFirebaseApp");

How can i programmaticaly set the auth of the secondary Firebase app instance equal to the default one (i'm using the Firebase UI Web to login)


Solution

  • So when initialising the secondary Firebase instance you can keep it global or find the instance using firebase.getInstance function.

    Anyhow you can authenticate the user for the secondary database as long as you have the reference to that db.

    const secondary = firebase.initializeApp(config, 'secondary');
    firebase.storage();
    await secondary.auth().signInWithEmailAndPassword(username, password);