How to use multiple accounts in Firebase Auth in Android? Using a single account works fine. But, when I try logging in with another account, the previous data is gone. How can this be done?
CodePudding user response:
Firebase Authentication has only a single current user per app instance. It does not support having multiple users signed in at the same time, so signing in another users automatically signs out the previous user.
What you can do is create a separate instance of the FirebaseApp
and FirebaseAuth
variables for each user, and then sign in each user into their own FirebaseAuth
instance.
Based on the documentation on configuring multiple projects in your Android ap, that should be something like this:
FirebaseOptions options = new FirebaseOptions.Builder()
.setProjectId("my-firebase-project")
.setApplicationId("1:27992087142:android:ce3b6448250083d1")
.setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
// setDatabaseURL(...)
// setStorageBucket(...)
.build();
FirebaseApp app1 = FirebaseApp.initializeApp(this /* Context */, options);
FirebaseApp app2 = FirebaseApp.initializeApp(this /* Context */, options);
FirebaseAuth auth1 = FirebaseAuth.getInstance(app1);
FirebaseAuth auth2 = FirebaseAuth.getInstance(app2);
We initialize both FirebaseApp
instances here with the same configuration data, since you have only a single project. But each instance can then have its own current user.