简体   繁体   中英

How to get user id(UID) from firebase realtime database without login

How to get user id ( UID) from firebase realtime database without login.

String currentuser = FirebaseAuth.getInstance().getCurrentUser().getUid();

Because this code works when any firebase user is loged in. For example if an user is loged in and he wants to get the UID of another user then how can he get the UID of that user.

在此处输入图片说明

I would NOT recommend you to share UID of any user that openly because it can easily be misused if exposed in the app to any user.

This may be a threat to the security of your database.

That said, if you're sure that this won't be a problem in your case, then getting uid of any other user can only be done when you're storing the uid of all the authenticated users in your database.

Suppose you have a JSON structure like this:

-FirebaseAndroidUniqueName
|
 -Users
  |
   -uid
   |
    -name
    -uid
    -otherThings

Then getting uid of any user can be done using a code like below:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
                    databaseReference.orderByChild("name").equalTo(userNameWhoseUidYouWant).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                               String uid = ds.child("uid").getValue(String.class);

                            }
                        }

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

                        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM