Sabbin
Sabbin

Reputation: 2445

Authenticate users between firebase apps

I have a React app with firebase auth and storage

Following on my previous question with multiple firebase apps here, I ended having multiple firebase apps inside my project.

I have the DEFAULT app initialized

firebase.initializeApp({
  apiKey: 'xxxxxxxxxxxxxxxxxxxx',
  authDomain: 'buckettest-xxxxx.firebaseapp.com',
  projectId: 'buckettest-xxxxx',
  storageBucket: 'buckettest-xxxxx.appspot.com',
  messagingSenderId: 'xxxxxxxxxxxxxxxxxxxx',
  appId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  measurementId: 'G-QZDBHEWHJ5',
});

Users log in with email and password, using signInWithEmailAndPassword method from firebase.auth() from the DEFAULT app above

In a component I create a secondary firebase app that connects to a different storage bucket as follows

const createStoreApp = (bucket) => {
  const firebaseStoreApp = firebase.apps.length === 1
    ? firebase.initializeApp({
      storageBucket: bucket.name,
      apiKey: 'xxxxxxxxxxxxxxxxxxxx',
      authDomain: 'buckettest-xxxxx.firebaseapp.com',
      projectId: 'buckettest-xxxxx',
      storageBucket: 'buckettest-xxxxx.appspot.com',
      messagingSenderId: 'xxxxxxxxxxxxxxxxxxxx',
      appId: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
      measurementId: 'G-QZDBHEWHJ5',
    }, 'storage')
    : firebase.apps[1];

  return firebaseStoreApp;
};

This solution works perfectly BUT there is the issue that there is no autheticated user on this secondary app.

Is there a way to autheticate the currentUser from the DEFAULT app instance to the second one without asking for credentials?

Upvotes: 1

Views: 107

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317828

Each initialized app instance has its own authentication. You can't have one app instance reach into another app to get its auth data. As such, you will need to use the Firebase Auth APIs to sign in the user in to the second app instance separately. Since you're using signInWithEmailAndPassword, it sounds like you're going to have to store the user's credentials somewhere until you're able to call initializeApp again with them as needed.

Upvotes: 1

Related Questions