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

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

Related

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

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.

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 can one fetch the authorized users along with their details in Firebase

I have a Firebase project, which I'm currently using with android. I need to programmatically fetch details of the users authorized along with the UID, Email, etc. Exactly the way it is shown in firebase (with the search), this web portal will be given to the vendor or the person using it to verify the user's authenticity.
I've attached the screenshot from Firebase, I'm hoping to replicate it the same way with the search. If this is possible, how do I go about doing this?
It's not possible to list users from an Android app, using only the Firebase Authentication SDK. You can list users using the Firebase Admin SDK, but that can only be run on a backend you control, using service account credentials for your project.
It might be easier if you store user info in a database, to be queried by client code, rather than try to have your app try to access auth data directly.

Firebase auth, is the user uid security sensitive?

I was wondering if getAuth().uid is sensitive/private in anyway? I am planning to use it on a user post something like: post.created_by: getAuth().uid. This makes writing rules/logic a lot easier.
The other way is to use the push id when the user is added to the database, which I'm trying to avoid.
No its safe to use the uid and recommended, firebase uses auth to authenticate the user and the assign the uid to identify the user across firebase.
You will be using uid in your security rules and as well as to identify user info in your db records.
Manually creating another id will render the efficiency of firebase useless.

Resources