0

I'm trying to check the user's registration if the username is being used by another user. But firebase doesn't have that option unfortunately they do have that option for email but not the username. I know how that is maybe because it is not in the user-auth but only in the database and has to be solved manually. Here this is the code I am using. only nothing is read in the database, but the toast message is displayed even with the names that are not used. and the registration continues even with the names that are used does someone have some kind of solution

    private void RegisterNow(final String username, String email, String password){
    auth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {

                    FirebaseDatabase.getInstance().getReference().child("MyUsers").orderByChild("username").equalTo(username).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            if (dataSnapshot.hasChild(username)){
                                Toast.makeText(RegisterActivity.this, "Username already in use please!", Toast.LENGTH_SHORT).show();
                                Intent i = new Intent(RegisterActivity.this, Login_Activity.class);
                                i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                                startActivity(i);
                                finish();
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {

                        }
                    });
  • Can you edit your question to show the data from your `MyUsers` node (as text, no screenshots please). You can get this by clicking the "Export JSON" link in the overflow menu (⠇) on your [Firebase Database console](https://console.firebase.google.com/project/_/database/data). – Frank van Puffelen Jul 28 '21 at 22:40

1 Answers1

0

It looks like you have a JSON structure like this:

"MyUsers": {
  "someIdForUser1": {
    "username": "userNameOfUser1"
  },
  "someIdForUser2": {
    "username": "userNameOfUser2"
  }
}

If that is the case, your check should be:

FirebaseDatabase.getInstance().getReference().child("MyUsers").orderByChild("username").equalTo(username).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()){ //  check if a result exists
            Toast.makeText(RegisterActivity.this, "Username already in use please!", Toast.LENGTH_SHORT).show();
            Intent i = new Intent(RegisterActivity.this, Login_Activity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(i);
            finish();
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); //  never ignore errors
    }

Note that the above can still lead to duplicate user names if multiple users register at the same time. The only way to prevent that is to have a separate node where you use the username as the key, as explained here: Firebase android : make username unique

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807