0

I'm really new to firebase. I've been reading similar topics here but I still don't get it.

Here's what I'm trying to do:

I made the parent key to uid and the key: value both to username cause I read that if you do that it'll not allow same keys but I don't understand, it still overwrites the key and values

My Firebase rules is set to read write true cause this is just a sample project. I hope someone can help me. Thanks!!

Jessica Rodriguez
  • 2,899
  • 1
  • 12
  • 27
Nikko Rod
  • 13
  • 5
  • Did you had a look at https://stackoverflow.com/questions/25294478/how-do-you-prevent-duplicate-user-properties-in-firebase – Rohith K D Dec 06 '18 at 04:10
  • yes, but I still cant seem to get it. – Nikko Rod Dec 06 '18 at 04:20
  • The Firebase Realtime Database does not have a mechanism to ensure the value of a certain property is unique across all child nodes. The accepted answer to the question Rohit linked shows explicitly to create a `/usernames` node, where the username is used as a key. Since keys are by definition unique in a node, this ensures each username can only be used once. Your JSON doesn't show the structure that Kato explained yet, which should look something like `{ usernames: { sample1: "Ourihel..." }}` to show that UID `"Ourihel...` has claimed username `sample1`. – Frank van Puffelen Dec 06 '18 at 09:09
  • Hey Nikko! did my answer help you solve your problem? Also, reply with @PradyumanDixit. – PradyumanDixit Dec 06 '18 at 13:05
  • Hello @PradyumanDixit sir, i think it will but i'm still trying to understand your answer and sir Frank's comment, i'm really new to this so i'm having a hard time understanding a lot of things, i'm sorry for being so slow at this . – Nikko Rod Dec 06 '18 at 13:10
  • No worries, it's great to start learning, Happy Coding! If you want to understand things from scratch, you may also refer https://github.com/Pradyuman7/ChattingDemoApp, also if you found my answer useful, consider marking it as correct by clicking the tick mark looking V type button next to it, it should turn green. This helps future readers of the question and I'd appreciate that too. Cheers! :) – PradyumanDixit Dec 06 '18 at 13:52

2 Answers2

0

Can you add your code? In my case

databaseReference.child("user_info").child(f_user.getUid()).child("username").setValue(f_user.getUid());

this code can prevent duplicate write.

but if you set code like this, I mean using push().

databaseReference.child("user").push().setValue(user);

this code cannot prevent duplicate write.

Sang Won
  • 52
  • 1
  • 1
  • 12
0

I would recommend you to make your database structure like following:

rootReference
|
 -- uid1
 |
  - username
 |
  - email
 |
  - other_details
|
 -- uid2
 |
  - username
 |
  - email
 |
  - other_details

This database structure not only would help you make every child unique but it would also help you search for user with particular username or email that you may need to get in future.

This database structure makes it easy to query with orderByChild() and also stores your username and email under uid.

For finding if a username is unique or not, you may use a simple query with orderByChild() like following:

reference.orderByChild("username").equalTo(userNameYouWantToSearch).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                            if(dataSnapshot.exists())
                             // do what you want
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) { // ToDo: do something for errors

                        }
                  )};

In if() statement, you can just tell that userNameYouWantToSearch already exists on database and hence is not unique.

PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
  • 1
    This has an inherent race condition: if two users register at almost the same time, the checks for both will say the username is available and then both may claim that name. The only proper solution is to add an additional node where the user names are used as a key, and then use security rules that only one user can claim each key/username. – Frank van Puffelen Dec 06 '18 at 09:11
  • Yes @FrankvanPuffelen, indeed this would be the case, but OP said that this was for demo purposes and he needed to just understand such things, that's why I gave a code that would be simpler to understand for demo purpose tests. – PradyumanDixit Dec 06 '18 at 09:42