-1

I have an android app in firebase where I manage users. I want to check, when I register a user in the app whether or not the user exists in the firebase realtime database before I register it. How can i do that??

The data structure in the db is something like this:

"users": {
    "some_user_id": {
        "name": "some_name"
        "username: "aaaa"
    },
    "some_other_user_id": {
        "name": "some_other_name"
        "username: "aaaa"
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Iván Guerra
  • 71
  • 2
  • 4
  • It looks like you're trying to enforce unique user names. In that case, see: http://stackoverflow.com/questions/29970681/enforcing-unique-usernames-with-firebase-simplelogin/29983839#29983839 – Frank van Puffelen Sep 01 '16 at 00:18

1 Answers1

0

Setup your firebase such as:

database.child("users")
        .addValueEventListener(this);

and check the result in the onDataChange function:

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
        boolean exists = false;
        for (DataSnapshot child : dataSnapshot.getChildren()) {
            Map<String, Object> model = (Map<String, Object>) child.getValue();

            if(model.get("username").equals("SET_YOUR_NEW_USER_NAME_HERE")) {
                exists = true;
                break;
            }
        }

        if(exists) {
            // This user already exists in firebase.
        }
        else {
            // This user doesn't exists in firebase.
        }      
} 
Mahmoud Ibrahim
  • 1,057
  • 11
  • 20