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?
Asked
Active
Viewed 615 times
2
-
What do you mean by "the previous data is gone"? Gone from where? – Alex Mamo Jun 24 '22 at 12:43
-
The previous account is gone from the device. And the new account's details come – Sambhav Khandelwal Jun 24 '22 at 12:51
-
I'm not sure I understand what "gone from the device" means. – Alex Mamo Jun 24 '22 at 13:32
1 Answers
3
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.

Frank van Puffelen
- 565,676
- 79
- 828
- 807
-
Hmmm. Ok. So, now how do I get the details for account 1 and account 2? – Sambhav Khandelwal Jun 24 '22 at 16:00
-
I'm not sure what you mean there. Did you try to create two instances of `FirebaseAuth` yet with the code I showed, and then signing the two accounts in with those? If so, what happened when you did that? Was there an error? – Frank van Puffelen Jun 24 '22 at 19:45
-
No. I was able to sign in. But, now how do I get the details for those accounts? Calling `FirebaseApp.initializeApp(this /* Context */, options);` again will give us another instance right? – Sambhav Khandelwal Jun 25 '22 at 03:42
-
1Your question was how to allow two users to sign in, which is what I showed how to do in my answer and you seem to have been able to replicate in your code based on that. If you have further questions, I recommend posting a new question with a [minimal repro](http://stackoverflow.com/help/mcve) so others can have a look too. – Frank van Puffelen Jun 25 '22 at 12:59
-
Never mind. The docs helped me. It says that i need to also give a third argument of name and then i can fetch it using that name. Thanks for it – Sambhav Khandelwal Jun 25 '22 at 15:02
-
You can also get the current user from `auth1.getCurrentUser()` and `auth2.getCurrentUser()` and pass those variables around. – Frank van Puffelen Jun 25 '22 at 23:52