-->

How to get userID by user Email Firebase android?

2019-02-12 18:33发布

问题:

Given a email address, is it possbile to get userID of a person? For example,

If I have a variable email that has email of the person. Can I get their ID by doing something like

String userID = mAuth.DatabaseReference.getuID.(email);

Sorry if this a stupid question.

Edit: note, I am looking to get ID of a person who is not the current user. So I can't use FirebaseUser user = mAuth.getCurrentUser();

The structure of my database looks like this, so the ID will be stored in database already. I just need a way of getting it (In the diagram presented below I don't have a field of email but I will be adding one, incase anyone was wondering -_-).

Also, if possible, I would like to get their profile image either based on their email or once I have gotten the id, through ID.

回答1:

If you want to look up a user by their email on a trusted server, you can use the Firebase Admin SDK. From the documentation on retrieving user data:

admin.auth().getUserByEmail(email)
  .then(function(userRecord) {
    // See the tables above for the contents of userRecord
    console.log("Successfully fetched user data:", userRecord.toJSON());
  })
  .catch(function(error) {
    console.log("Error fetching user data:", error);
  });

The client SDKs for Firebase Authentication only provide access to the profile of the currently authenticated user. They don't provide a way to look up a user by their email address. To allow client-side lookup, the common way to do so is to store the UID-by-email-address in the database:

"emailToUid": {
    "SumOne@domain,com": "uidOfSumOne",
    "puf@firebaseui,com": "uidOfPuf"
}

With this simple list, you can look up the UID by the encoded email address from:

ref.child("emailToUid").child("SumOne@domain,com").addSingleValueEventListener(...

See also:

  • Firebase Authentication Service - lookup uid from email without login
  • Add Extra Details on Firebase User Table
  • How to get authenticated users from firebase database?


回答2:

Firebase do not provide user info on the basis of email. You can do one thing make login with your credential and after successful login use below code to get details of user.

FirebaseUser currentFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
                            if(currentFirebaseUser !=null)

    {
        Log.d(TAG, "onComplete: currentUserUid---->" + currentFirebaseUser.getUid());
        Log.d(TAG, "onComplete: currentUserEmail---->" + currentFirebaseUser.getEmail());
        Log.d(TAG, "onComplete: currentUserDisplayName---->" + currentFirebaseUser.getDisplayName());
    } else

    {
        Log.d(TAG, "onComplete: currentUserUid is null");
    }


回答3:

This way you can login user with email and password and retrieve id:

FirebaseAuth.getInstance().signInWithEmailAndPassword(email,password).addOnSuccessListener(new OnSuccessListener<AuthResult>() {
          @Override
          public void onSuccess(AuthResult authResult) {
              FirebaseUser user = authResult.getUser();
          }
      });