Authenticate users between firebase apps

℡╲_俬逩灬. 提交于 2021-01-29 00:55:20

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/65287720/authenticate-users-between-firebase-apps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!