One Firebase authentification for multiple non-permanent users - is it possible? - firebase

In a classroom, I'll provide to 100 users, 100 android devices, where they'll have to open an UNITY app.
Inside the app, I don't want the users to bother to enter any login, any email, any password.
So I guess they would need to be connected "automatically" to the same firebase account.
Then, they'll need to select one of the avatars available in firestore, with which they'll play and the app will save their avatar progress in firestore.
Is it possible to have so much players and devices using the same firebase account at the same time for that purpose ?

I'd probably first look at signing in each device anonymously. That way each device/app instance has a unique, persistent identifier, without having the user enter any credentials.
You can then also use the UID in your database to have indicate the avatar that each user/device claims, and in the security rules of that database, to ensure users can only access data they're authorized for, and for example that each avatar can only be claimed by one user.

Related

Send local personalized notifications without login

I have been trying to find an answer for a while now. I am creating a react native App, and want to send personalized notifications to a user. I only want to ask the user for their name and number, and don't want to make them create an account. Is there a way i can save this info in firestore?
Right now for firestore the only way I see to save user data is to first make them sign in.
auth.createUserWithEmailAndPassword(email, password)
I just want to be able to get a users name and save it in a database, to then be able to send notifications like "Good Job John Doe". My app has no need for an email and password auth.
You can use anonymous authentication to create temporary anonymous accounts. This can be linked to a user if they decide to sign up to your app.
You can use firebase firestore to store user information without using authentication.
Just simply ask the user about their name and number, save it in the firestore. Then using Firebase Cloud Messaging subscribe to a topic which will be unique to the user (name+number or something like that). Then using firebase cloud function or manually you can send notifications to each individual user

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.

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.

what is the best way to manage FCM registration ID in database server?

I'm trying to use firebase cloud messaging service for my android application and I'm trying to find the best way to manage registration ID in database server.
I was thinking to create new table with userID,registrationId (where userID is unique for each user) in my database and insert new record once the user logs in successfully and remove that record when the user logs out. but there are some situation that the registration Id will be refreshed, I can get the new registration Id to save it in the database. but how can I get the old registration Id to remove it?
Are there better way to manage the registration Id in database?
note: a device can access one account but there are might be many devices that use the same account.
Depending on the user, you might want to also have an identifier for each device they use. But for a simpler explanation, I'll go with the scenario where each user only has a single device.
If you're using Firebase Database, then the simplest way to structure the nodes would be something like this:
pushTokens/
$userId: <registration_token_here>
Simple as that. You just pair the userId that you use in your app (possibly for authentication) and place the token there. On sign out, log the user out. When the user is currently signed-in and the token refreshes, handle it in onTokenRefresh(), send the new token to the DB, and replace the older one. Deciding to keep the old one for logging purposes is your call.
Possibly helpful posts:
Firebase Cloud Messaging - Managing Registration Tokens
Managing FCM device groups

Best practice Fetch user's information from the Firebase User UID

I have integrated Firebase's Facebook and Email Login into my iOS application.
Although, I would like to only store the Firebase User ID in my API database.
Therefore from my backend server (say Ruby) how can I query firebase to get say the user's name and user's email address given that I have their Firebase User UID.
Thanks
To directly read user data for arbitrary users of your app, you'd use the Firebase Admin SDK: https://firebase.google.com/docs/admin/setup. This specific page shows how to read user data: https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data
Reading user data through the Admin SDK is currently only available for Node.js and Java. Since Ruby isn't on this list, you'll need to keep the user information elsewhere. The Firebase Database is a common place to store such data, so that any platform can access it through its REST API.

Resources