I am trying to create a registration activity inside a draft chat application using android studio. At first I am checking if all the fields of the form are filled and then I am at the point where I want to check if the desired username of the user is already inside the database and reject that request with a message.
This is the function I managed to assemble together:
private void checkifUsernameExists(final String username, final String email, final String password) {
Query usernameQuery = FirebaseDatabase.getInstance().getReference().child("users").orderByChild("username").equalTo(username);
usernameQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
Toast.makeText(registerActivity.this, "Username already exists", Toast.LENGTH_LONG).show();
} else {
message.setTitle("Registering user");
message.setMessage("Pleases wait while we create your account");
message.setCanceledOnTouchOutside(false);
message.show();
registerUser(username, email, password);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
When I am at the point where I click the register button it gives me an error inside the logcat
saying:
"W/SyncTree: Listen at /users failed: DatabaseError: Permission denied"
Its like I dont have access to that specific value of the database.
These are the rules inside the db:
{
"rules": {
"users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
I did tried to modify the db rules but it was giving me an error.