简体   繁体   English

Firebase从电子邮件获取uid

[英]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. 我想从输入电子邮件中获得一个uid,以便可以通过电子邮件添加朋友。

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. 如果我直接输入一个uid,但我希望输入的内容为电子邮件并从中获取一个uid,则此方法有效,或者如果有其他解决方案,请提出建议。

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. 出于多种原因,最好保持Firebase数据结构平坦,并将用户个人资料与好友列表分开。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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