简体   繁体   中英

Firebase get uid from email

I would like to get a uid from inputing an email for making it possible to add friends with an email.

Here is what I have gotten so far:

final DatabaseReference mRef = FirebaseDatabase.getInstance().getReference().child("users");
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    if (user != null) {
        final String currentUserUid = user.getUid();

        mRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.hasChild(uid)) {
                DatabaseReference newRef = mRef.child(uid);
                newRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if (!dataSnapshot.hasChild(uid)) {
                            DatabaseReference database = FirebaseDatabase.getInstance().getReference();
                            String username = String.valueOf(dataSnapshot.child("username").getValue());
                            String email = String.valueOf(dataSnapshot.child("email").getValue());
                            String firebaseToken = String.valueOf(dataSnapshot.child("firebaseToken").getValue());

                            Friend friend = new Friend(uid, username, email, firebaseToken);
                            database.child(Constants.ARG_USERS)
                                    .child(currentUserUid)
                                    .child(Constants.ARG_FRIENDS)
                                    .child(uid)
                                    .setValue(friend)
                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Void> task) {
                                            if (task.isSuccessful()) {
                                                mOnFriendDatabaseListener.onSuccess(context.getString(R.string.friend_successfully_added));
                                            } else {
                                                mOnFriendDatabaseListener.onFailure(context.getString(R.string.friend_unable_to_add));
                                            }
                                        }
                                    });
                        } else {
                            Log.d("Adding Friend", "User already not exist");
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
                } else {
                    Log.d("Adding Friend", "User does not exist");
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

This works if I input an uid directly but I would like to have my input be an email and get an uid out from it, or if there are other solutions please suggest.

This is how the database structure looks like: Firebase DB的图像

Thanks for the help in advance.

To search for a user by their email address you need to order/filter on that property:

Query query = mRef.orderByChild("email").equalTo("example@example.com");
query.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // There may be multiple users with the email address, so we need to loop over the matches
        for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
            System.out.println(userSnapshot.getKey());
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Getting Post failed, log a message
        Log.w(TAG, "find email address:onCancelled", databaseError.toException());
        // ...
    }
});

Not directly related to the question, but please restructure your data to remove the nesting of friends and profiles. For many reasons it's best to keep your Firebase data structure flat and separate user profiles from friends lists.

/users
  uid1: ... profile for user 1
  uid2: ... profile for user 2
/friends
  uid1: ... friends list for user 1
  uid2: ... friends list for user 2

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