This question already has answers here:
Firebase android : make username unique
(4 answers)
Closed 2 years ago.
In my app, user should be able to connect with each other. So to identify each other, they should have some human readable property, which they can share easily. I was wondering if there is any way that, I could have a Human readable "username" similar to Uid?
I was initially planning to save an Email-id of the user in the database, but as it can change, this approach is not recommended. Also, this approach will fail if user logs in using their phone number.
So, is there any in-built Firebase mechanism to this? What are the best practices / design patterns here? What is the commonly used approach in Firebase world, when it comes to 'identifying each other' or 'sharing'?
It's similar to my answer here.
You need to keep a separate node in firebase realtime database regarding that.
So if your user tries to connect with any other user name user123, you will need to find the node user123 in database and read the UID which will be stored inside and then proceed to your further actions.
Alternatively to make it less lengthy, you could use usernames instead of UID, so there is no question of UID in the application but you will need to take care there are no same usernames and could increase complications.
I'll prefer the first method. Do let me know if you need more assitance.
Related
I am trying to create a basic app for my small educational business.
We supply English teachers to schools and I want a way for parents to access the progress reports and other data about their children.
I'm using Android Studio and Flutter as well as Firebase to store the data. I don't want parents to be able to access the data of every child, obviously, but they may have more than one child at the school.
So I need to limit their read privileges to just the records that relate to their children. I'd like to do that by giving them some sort of registration code that they could use the first time they access the app so that we can ensure that they are only being given access to the correct records. Subsequent logins would then be via email and password.
Is there a way to do this with Firebase?
Your use-case sounds feasible, but it is really broad which makes it impossible to answer it completely-yet-succinctly.
Specific on what to do with the registration code that associates a parent with their children, when you generate the registration code, you write that code and the associated children to a database. Then when the parent registers with that code, you associate their account (UID) with the code. You'll just have to ensure that the code is sufficiently long and random that it can't be reasonably guessed by another user.
I have quiz game and I want to add 1000 questions on firebase firestore and I have thousands of users in my game and every user has special id.
What is the best way to know if the user answered this question before or no?
Should I add user id inside every question who answered it or there is the another way?
I want build data structure logically for reduce consumptionmy read limit on firestore.
The two most common solutions I can quickly think of:
Create a single top-level collection answers, where you store all answers for all users. In that collection use the combination of UID and question ID to name each document, so that you can check if a document /answers/$uid+$questionID exists.
Create a top-level collection users in which each UID represents a user. Under each UID document create sub-collection answers, where you use the question ID to name the documents. Now you can check for each user if they answered a specific question, by checking /users/$uid/answers/$questionId.
Neither of these is pertinently better than the other, and they typically allow for similar use-cases. I'd usually pick the second structure simply because it results in a more natural distribution of the data, but it's s pretty small preference in practice.
I'm working on a chat client using the firebase realtime database as the database. The way that it currently works is that it saves a chat log between two people in a chat collection with each entry in the following format <uid>-<uid>. This works great as it just looks your uid and the uid of the person you want to chat with and then sorts them, so it's always a consistent format and then it looks if that entry exists on the chat collection and if so, it just adds to that entry. Otherwise it creates a new one.
This works awesome. I'm trying to think ahead though if we want to be able to have multiple people talk together like in slack. I could just add 3 or even 4 people's uid as the key but eventually it's going to be insanely long. The limitation of a firebase key is 768 Bytes. Apparently that's somewhere between 500 and 700 characters. I doubt we will have the key get that long, but if we can figure out a solution that is more scalable now and won't require us to fix our data later, i'd rather do that.
I was thinking that each chat entry could have a participants array with the uid's of all the users in that chat. Then if you want to chat with someone, we would need to query all chat entries and check the arrays in each of them for the current user uid and the uid of the person(s) they want to chat with. That doesn't seem very efficient though.
Any thoughts on which implementation is better / more scalable / performant? Or perhaps a suggestion for another implementation?
How about simply using the hash of the resulting concatenation of UIDs?
Alternatively:
Come up with your own unique room key, e.g. using a push ID.
create a new top-level node with chatroom-keys and store the concatenated UID as the value there:
chatroom-keys
push-id1: uid1-uid2-uid3
push-id2: uid1-uid2-uid3-uid4-uid5-uid6
push-id3: uid3-uid4-uid5-uid6-uid7-uid8-uid8-uid10
In this structure you can look up the room key for a set of participants by:
firebase.database().ref("chatroom-keys").orderByValue().equalTo("uid1-uid2-uid3")
coming from a SQL background I am still getting used to Firebase.
If I have a node for customers and I allow Firebase to create an id by using the "push" method, how do I take a customer and then only push if it doesn't already exist with the same email address and if it does, then update it?
I know set will create or update if I have a specific node (ie each node is identified by, say, the email address) but in this case I want to push if it doesn't exist and presumably set if it does. What's the neatest syntax for doing this?
thanks
Phil
First, Firestore is recommended for projects going forward. More features.
Note that firebase auth has this email duplication functionality baked in already. So, you might be spending effort on a feature that is already done for you. See auth/account-exists-with-different-credential
Finally to answer your specific question:
The way I do that is to chain the write after a read (.then). The read seeks to fetch the node. After the read if whatever field exists or === myString then you know it's already there.
I'm trying to implement a rule in my firebase DB that dis-allows a user to update a posts likes value if it doesn't already exists in their fanned data. Consider this file structure.
Notice how there is a full dictionary of values. Given conditions in my app, users have the ability to write to the likes value in this dictionary. In the event that this dictionary of data Does not already exist, I want to prevent users from writing to the likes value so this doesn't happen.
This is the security rule I have currently in place but I am not sure how I can solve this problem.
I have already looked at the documentation and watched David East's video on securing your Firebase DB but I am still lost.