0

I am using Firebase to keep my users in database.

for now it is like this:

enter image description here

I want to make it unique with rules.

Rules:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

Also, I am creating users like this:

private void createUser(String uname){
    Map<String,Object> newUser = new HashMap<String, Object>();
    newUser.put("uname",uname);
    DatabaseReference dbref = firedb.getReference();
    dbref.child("Users").push().setValue(newUser);
}

Then calling it like this:

createUser(name);

As a result, I want to force unique for "Users-->uname" in my database.

How to force unique by rule?

EDIT

I changed my createUser function but there is another problem for now. I did like this:

    private void createUser(String uname){
        Map<String,Object> newUser = new HashMap<String, Object>();
        newUser.put("uname",uname);
        // newUser.put("soyad","soyad");
        DatabaseReference dbref = firedb.getReference();
        //dbref.child("Users").push().setValue(newUser);


        dbref.child("User").child(uname).setValue(true, new DatabaseReference.CompletionListener() {

            public void onComplete(DatabaseError error, DatabaseReference ref) {
                System.out.println("Value was set. Error = "+error);

                //Here, always working even if there is no same username

            }
        });
    }

But, now oncomplete always working although I write different username.

Logcat always like this(if I write same username or different username, same result)

05-22 13:37:56.611 3925-3925/xxx I/System.out: Value was set. Error = null
05-22 13:38:11.551 3925-3925/xxx I/System.out: Value was set. Error = null
05-22 13:38:19.641 3925-3925/xxx I/System.out: Value was set. Error = null
05-22 13:38:44.981 3925-3925/xxx I/System.out: Value was set. Error = null
05-22 13:42:17.091 3925-3925/xxx I/System.out: Value was set. Error = null
05-22 13:42:27.741 3925-3925/xxx I/System.out: Value was set. Error = null
05-22 13:42:34.491 3925-3925/xxx I/System.out: Value was set. Error = null

Finally, how can I check if the username exist in database?

  • There is no way to enforce a specific value to be unique within a set of nodes. On the other hand: keys are by definition unique in their parent. How to create a system of unique user names based on this has been discussed regularly before, so instead of repeating, I'll link you to a few of those. https://stackoverflow.com/questions/35243492/firebase-android-make-username-unique, https://stackoverflow.com/questions/45399151/only-allow-unique-values-in-firebase-realtime-database – Frank van Puffelen May 21 '18 at 18:39
  • I looked these links but I could not understand what should I do. – Gürkan Çatak May 21 '18 at 20:26
  • You'll need to create a `/usernames` node, where you use the unique user names as keys (and for example the UID as the value). – Frank van Puffelen May 21 '18 at 20:29
  • I have users node as you see in the picture. How can I do this without adding new node? Because I have already. – Gürkan Çatak May 21 '18 at 20:38
  • You will need to add a node with the user names, that you purely use to guarantee the uniqueness of the user names. Under that node you use the user names as the **keys**, not as the values. So `dbref.child("Users").child(newUser).setValue(true);` – Frank van Puffelen May 21 '18 at 20:55
  • Thanks it is working but if I try to add same username, nothing displayed no error. How can I check? I want to check firstly if this username exists. Your code works and does not allow to create same usernames but I want to check if there is a username with same, how can I check? – Gürkan Çatak May 22 '18 at 10:29
  • Frank van Puffelen, I edited my question can you look again please? – Gürkan Çatak May 22 '18 at 10:45

0 Answers0