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

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.

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.

getting user id from phone number in flutter

in the app, which logs the user in only through phone authentication.
now the question is can I get the user id from his phone number(not the user id of the current user though).
I have the access to other users phone number in the app, and I want to get their id for further use.
is it possible in flutter with firebase at the backend?
Looking up the UID for a user based on either phone number or email address is considered a sensitive operation. For this reason such operations are only available in the Admin SDKs, which are designed to be used in trusted environments, such as your development machine, a server you control, or Cloud Functions. For more on these, see the bottom two code samples in looking up user data in the documentation.
If you want to perform such operations from your client-side code, your two main options are:
Store the required mapping in a cloud-hosted database, such Firebase's Realtime Database or Firestore, as Huthaifa also answered.
Create your own custom API on a server or Cloud Functions where you lookup the user through the Admin SDK. Your client-side application code can then call this custom API.
In both of these cases you are in full control of what data you share, and how you secure access.
Sure you can, there are multiple approaches for this, if you post your structure for the user model, or how you are storing them in firebase.
You can run a simple Firebase query like this example:
FirebaseFirestore.instance.collection('users').where('phoneNumber', isEqualTo: thePhonenumberOftheuserYouwant2GetUIDfor).get()
You can also store the available\accessible contacts for every user in their user document, and when your user logs in, it'll fetch all their allowed user numbers.
The more information you provide to your problem, the more StackOverflow can provide you back.

Flutter get User Data from Firebase

I want to get User Data from firebase, I need the diplayName of a User. is there any way to get the displayName of a other user with his uid?
There is no way to look up information about another user in Firebase Authentication by using the client-side SDKs of Firebase, as that would be a security risk.
There are two common ways to allow searching the users in a secure way:
Write information about each user to a database (such as Cloud Firestore or the Realtime Database) when they register, and then search the database when needed. That way your code controls what data gets written and thus is searchable.
Firebase has Admin SDKs that run in trusted environments, such as your development machine, a server you control, or Cloud Functions. These SDKs have options to list users, which means you can search them. If you wrap one of the Admin SDKs in a custom API that you build and secure yourself, you can then call that from your Flutter code.
Also see:
React native firebase authentication searching
You can't get the name, or any other details of a user that is not currently signed in using FirebaseAuth.
Instead, you must create a node in your database where you store the name, and any other necessary details by querying the database.

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.

Firebase Multiple Accounts are signed at the same time

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.

Resources