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的,或者有請提出其他解決方案。
這是數據庫結構的樣子:
感謝提前的幫助。