Firebase Multiple Accounts are signed at the same time - firebase

I want my users to sign in with multiple accounts (Not linking multiple providers)
like gmail, it allows you to use multiple signed in accounts at the same time
you can check all inboxes for all accounts at the same time
e.g. I am a User and I want to use two accounts
john#example.com & john21#example.com
I want both emails signed in (there are two auth tokens in this case)
I have searched about it in Firebase docs, I couldn't find anything
Is this possible in Firebase Auth?

From one project, no i don't think you can have two users signed in.
You can create another Firebase project and add your app there (you'll be able to add your sha-1 on either one). You can then look up their approach to using two Firebase projects in one app.
You'll be able to sign in with two users in this way if there are two projects to sign in with. You'll have to manage your users across both the databases on your own i.e same user will have to have an account with same credentials made in both the projects.
It's complicated but it can be done. I use anonymous sign in from two separate projects simultaneously in my app, both users are the same... Just their projects are different.

I was thinking about this issue, reading through the documentation and found that you can re-authenticate a user by credential. So maybe you can do it this way:
when a new user wants to sign in you take his info as AuthCredential object like this:
AuthCredential credential = EmailAuthProvider.getCredential("user#example.com", "password1234");
//there is also GoogleAuthProvider, FacebookAuthProvider...
then to actually signing him in you use signInWithCredential(AuthCredential) :
FirebaseUser user = FirebaseAuth.getInstance().signInWithCredential(credential);
Now you need to save the credential object and the user object somewhere safe.
If you want to add new user you do the same and save the new objects.
After that whenever in your app you want to switch between the users you get that user's object and call reauthenticate(AuthCredential) method:
myFirstUserObject.reauthenticate(myFirstUserCredential);
I didn't really test this, you think it could work? I don't know exactly how to save the objects and if there is a safe way to implement it without exposing each user's credential?

Multiple simultaneous logins in the same project is not supported with Firebase Auth.
You could possibly have multiple logins between multiple projects if you manually initialize the Firebase SDK with the settings for the other projects.

Related

Use Firebase Auth data and users for multiple projects and apps

If I have multiple apps with Firebase (different types and kinds) but want to be able to have 1 user base for all of them (1 account for all apps, even if they only use 1) how would I do this? I was thinking either 1. cloud function (every time someone signs up, add them as a user in the other apps) or 2. A separate project for Auth only and configure both of them in the app, but that might mean requests to firestore, storage, and functions aren’t authorized. NOTE: I'm willing to use GCP products separate from firebase to accomplish this.
If the apps are all part of the same suite, you can add them to a single Firebase project. This is by far the easiest way to do this, but there is a hard limit on how many apps you can have in a project in this way (according to the FAQ, this is 30 at the moment).
If the apps are not all part of the same suite, or you need more than the limit, your only option is to use custom authentication. This means you'll create a custom authentication provider that takes the user's credentials, verifies them, and then creates a UID and token for that user to the client which then uses it to sign in to Firebase.
In the back-end you could possible use a single Firebase project for then generating all these users, although I'll admit it's been a few years since I did that.

Flutter - Understanding Firebase Admin and how to get a user's information from email/uid/name

I'm making a little Snapchat clone, and a part of this app I'm trying to build is the ability to add a friend and start a conversation with them. I'm using Firebase to manage my users and I'm a little stuck now trying to figure out what works and why I'm getting problems trying to use some methods or functions.
What I want is this simple line of code to work:
var userByEmail = await _admin.app().auth().getUserByEmail("b#gmail.com");
print(userByEmail.toString());
However this has been giving my some problems, most recently, the following error message:
Unhandled Exception: FirebaseAuthError(auth/invalid-credential): Must initialize app with a cert credential or set your Firebase project ID as the GOOGLE_CLOUD_PROJECT environment variable to call verifyIdToken().
Getting to this point made me want to first ask a question about FirebaseAdmin and Auth before continuing and potentially screwing up my app settings.
Is there a simple way to do what I'm trying to do?
I have a Firebase.instance.initializeApp() in my Main function, do I only ever call that once or should I start initilizeApp in the initState of each Stateful Widget where needed?
What does this error message actually mean?
You are trying to use the Firebase Admin SDK in your Flutter code, which is not possible. The Admin SDKs give full administrative access to your Firebase project, which would be a serious security concern if you allow that in your Flutter app.
If you want to allow certain administrative functionality in your application, you will have to make that functionality available yourself. For example, to look up a user by their email address, there are two common approaches:
Store the minimal information about each user in a cloud-accessible database (such as Firebase's Realtime Database or Cloud Firestore) when each user registers with your app, and then look it up from there.
Wrap the getUserByEmail from the Admin SDK in a custom API that you make for yourself, on a server you control or in Cloud Functions. In that API you validate that the user making the call is authorized to do so, then call Firebase through the API you were trying to use, and return the minimal result back to the caller.
Both of these are feasible and can work to solve a variety of use-cases. But if you've never built backend code before, you might find the first approach easier to get started with.
Also see:
How to get Firebase UID knowing email user?
Flutter get User Data from Firebase
The right way to do what you want is using Firebase auth, authenticating your user and using a collection to store and retrieve users information. That auth information provided by firebase should only be used for authentication and security purposes.
The Firebase admin must have a user logged in to work properly, but its purpose is to provide a more administration environment and should not be used inside a clients app, unless its an admin app.
With all that said, lets go for the rescue:
Authenticate your user (using firebase auth);
After auth, save all the user information you want to share with other user inside its own collection (you will need to create one);
When an authenticated user (this is important) 'request any other users data, you query for the data in the previous created collection.

How to allow user to create and switch between multiple accounts in Flutter?

I am trying to make a Flutter app where the user can sign in into multiple accounts (different email IDs) and can switch between them from the UserAccounsDrawerHeader. For example, in Gmail app, users can switch between multiple Gmail accounts. Is this possible using Firebase Auth for Flutter?
In the default scenario, Firebase Auth generally does not support allowing a user to be signed in with multiple accounts at the same time. If you want to add support, what you will have to do is use initailizeApp() to initialize a new App instance - one for each user account, and sign in the user to each one of them. You will then have to pass that app instance around to the other Firebase APIs to use that account for authenticated access (for example, Firestore queries).
To be honest, it's not clear to me from the provided APIs how to do that last part. but perhaps Firestore.getInstance(app) might do it.
In any event, it is not trivial to implement. There is not a simple configuration or trick that will allow multiple simultaneous sign-ins. Usually apps just make the user sign out, then in again with another account.

Building two (Client/Admin) apps on one firebase authentication

We have two apps, one for the client and one for the admin (similar to Uber's user and driver apps). In both apps, when users attempt to authenticate, cloud functions triggers a new user creation and it saves a document in users' collection in cloud firestore with some data, like phone number, email, etc. We are encountering a an issue that when the user or the "driver" registers, the data model that is saved in the cloud firestore has different fields and they are different from one another.
When a new user is registering, how can I trigger in the cloud function that can identify whether it is from the "user" or the "drive" app?
There's not going to be anything in the Cloud Functions auth trigger that tells you which app registered the new user. Within a Firebase project, all user accounts are effectively shared between all apps, and all users will have the same permissions between those apps.
If users slot into different categories based on the app they used to sign up, you'll have to create that distinction on your own, perhaps with something you write to a database. If this isn't acceptable from a security perspective, you should use two different projects instead to keep everything separate.

How can I create a new user via Firebase Auth without signing in?

I'm working on an enterprise application in which administrators create new users (there is no sign-up form). I am using Firebase Auth, which is great in many ways, but I've come across a problem for my use case. When you use firebase.auth().createUserWithEmailAndPassword(email, password), the user thereby created is automatically signed in. This obviously won't work when an admin is creating the user accounts. Is there any way to avoid this behavior? Thanks.
This is a good use case for the Firebase Admin SDKs. Instead of creating the user client-side, you create the user in a managed environment, like a server or Cloud Functions. You have the client make a call to your endpoint when you want to add a new user. This codelab shows how to incorporate custom claims using the Firebase Admin Auth SDK. This is a little different from what you're exactly looking for, but it can get you started in the right direction.

Resources