How to get userID by user Email Firebase android?

前端 未结 5 1739
夕颜
夕颜 2020-12-29 11:58

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 so

5条回答
  •  悲&欢浪女
    2020-12-29 12:09

    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?

提交回复
热议问题