2017-06-17 474 views
0

我想通過輸入電子郵件來獲得uid,以便通過電子郵件添加朋友。Firebase通過電子郵件獲取uid

這是我迄今爲止得到:

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) { 

      } 
     }); 
    } 

這工作,如果我的UID直接輸入,但我想有我輸入的是電子郵件,並從它那裏得到一個UID的,或者有請提出其他解決方案。

這是數據庫結構的樣子: Image of firebase db

感謝提前的幫助。

回答

0

要搜索用戶通過他們的電子郵件地址,您需要訂購該財產/過濾器:

Query query = mRef.orderByChild("email").equalTo("[email protected]"); 
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()); 
     // ... 
    } 
}); 

不直接相關的問題,但請重組數據刪除的朋友和配置文件嵌套。出於許多原因,最好將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