0

I am trying to check uid from firebase Realtime database, but its only returns the current uid and when I try to check uid with previous uid then also it only returns the current uid. I had tried many ways and searched but I can't get the exact solution, please if any one could help.

here what I am using to check the uid

FirebaseAuth.getInstance().getCurrentUser().getUid();

here is my code how I am trying to check

 String userEnteredUserName = binding.textLoginUserName.getEditText().getText().toString().trim();
 String userEnteredPassword = binding.textPassword.getEditText().getText().toString().trim();
 DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
    Query checkUser = reference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).orderByChild("userName").equalTo(userEnteredUserName);
    Log.d("uids", "uid: " + uid);
    checkUser.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            binding.textLoginUserName.setError(null);
            binding.textLoginUserName.setErrorEnabled(false);
            Log.d("users", "Username: " + snapshot.toString());

            if (snapshot.exists()) {
                String passwordFromDB = snapshot.child(userEnteredUserName).child("password").getValue(String.class);
                if (passwordFromDB.equals(userEnteredPassword)) {
      //next activity
}else{
Toast.makeText(Login.this, "Error", Toast.LENGTH_SHORT).show();
}
        }

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

            Toast.makeText(Login.this, "Error", Toast.LENGTH_SHORT).show();

        }
    });

In log what I get,

2022-02-06 13:13:06.173 18093-18093/abc C/uids: uid: OFtpR6bfISP3Odd9K1oGWCQmeEf2

Here is my firebase data, "aisha12" is working which is under the current uid but when I try to check "vgbb" it returns only the current uid enter image description here

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Aisha Kumari
  • 193
  • 2
  • 9
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Feb 06 '22 at 09:18
  • yes, whenever I check userName, using this "FirebaseAuth.getInstance().getCurrentUser().getUid()".my code only checks username only under this uid "OFtpR6bfISP3Odd9K1oGWCQmeEf2" and when I try to check another username under this uid as example, "3l6RIm1ACacB4WOEiq5k7nrIk8b2" but it only checks under this "OFtpR6bfISP3Odd9K1oGWCQmeEf2". The problem is that I want to check different different username under different uid. can you please help me out.@Alex Mamo – Aisha Kumari Feb 06 '22 at 10:01
  • what is the code you tried for getting the value of other users – Sambhav Khandelwal Feb 06 '22 at 10:19
  • this is the code " Query checkUser = reference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).orderByChild("userName").equalTo(userEnteredUserName);" @ Sambhav Khandelwal – Aisha Kumari Feb 06 '22 at 10:21
  • When using `FirebaseAuth.getInstance().getCurrentUser().getUid()` you are getting the UID, not the user, right? So what exactly would you like to get? – Alex Mamo Feb 06 '22 at 14:27

1 Answers1

1

If I understand correctly, you are trying to allow a user to register a username and ensuring that the username is unique. Your current data structure doesn't allow you to do that query though.

Firebase Realtime Database can only order/filter on a value that is at a fixed path under each child node of the location you query. So in your current code:

reference.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).orderByChild("userName").equalTo(userEnteredUserName);

You are querying the direct child nodes of /Users/$uid looking for the username under there. Since you're specifying the UID in that path, you're only searching under that specific user.

There is no way with your current data structure to search across all /Users, since the property you are looking for is under /Users/$uid/$username/userName, so with two dynamic keys in there, and the database can only handle one dynamic key.


To allow the query, you will need to change the data structure and remove the $userName level from it, so that you get:

Users: {
  "3l6Rm....": {
    "userName": "vgbb",
    ...
  },
  "OftpR...": {
    "userName": "aisha12",
    ...
  }
}

Now you can search for the username with:

DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");

Query checkUser = reference.orderByChild("userName").equalTo(userEnteredUserName);

I also recommend checking out these previous questions on allowing a user to register a unique username:

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thank you for the explanation @Frank van Puffelen but I have one question that can I check User name under uid Users: { $uid:{ userName:......... } } – Aisha Kumari Feb 08 '22 at 11:36
  • Once you had modified the data structure as shown in my answer, that's what the `Query checkUser = reference.orderByChild("userName").equalTo(userEnteredUserName);` in my answer does. – Frank van Puffelen Feb 08 '22 at 15:07