Sagaryal
Sagaryal

Reputation: 415

How to manage multiple users in android

I am developing an android app which uses firebase authentication. I can login and logout a user at a time. Now my question is I don't want users to logout and enter credentials every time for another user to login. I want to allow users to use their accounts exactly like how Gmail implements it. In Gmail as we know, to view emails from different accounts, we just need to enter the credentials one time and we can just view emails by swiping from left and selecting the account. How can I do that?

I am sorry I am not even able to figure how to ask this question in google. Any link or guidance on how to implement this would be very helpful. Thankyou

Upvotes: 2

Views: 1051

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 600126

Most Firebase products (such as Authentication and the Realtime Database) are tied to a FirebaseApp object for their configuration. This means that if you have multiple FirebaseApp instances, you can get a separate FirebaseAuth from each, and authenticate a different user on each.

FirebaseOptions options = new FirebaseOptions.Builder()
    .fromResource(this.getApplicationContext())
    .build();

FirebaseApp.initializeApp(this /* Context */, options, "secondary");

FirebaseApp secondary = FirebaseApp.getInstance("secondary");

// Get the auth for this app instance
FirebaseAuth auth = FirebaseAuth.getInstance(secondary);

With this you can have a separate signed in user for each FirebaseApp object.

Upvotes: 3

Mohammed Rampurawala
Mohammed Rampurawala

Reputation: 3122

With FirebaseAuth object you can check if the user has logged or not.

If the currentUser is null in the FirebaseAuth then user has not been logged in yet.

If you want to logout the user then just call firebaseAuth#signout.

Upvotes: -1

Related Questions