How do I get the email address of a Firebase user by ID [duplicate]

寵の児 提交于 2021-02-07 11:12:23

问题


I'm using email/password authentication in Firebase. So every user has an email address. My users are in groups, and I want each user to be able to see all the other users in the group. Specifically, I want to display a list of emails in the frontend to the user. My question is not about how to make rules for that, but rather, how do I get a user's email addresses given a user ID? Note that I'm asking about getting a list of other users, not the currently signed in user.

I haven't found any SO answers showing how to get the email address of another user via auth(). I've seen suggestions to place the email in the /users collection under the user ID, but that seems incredible brittle to me to store the email addresses in both auth() and /users/$userId. Changing email addresses will be a nightmare. So there must be a way to get the emails from auth(), right?

Thanks!


回答1:


If you want to retrieve the email address of a specific user, you can use the new Firebase Admin SDK.

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

See the documentation on retrieving user data.

As its name implies, the Admin SDK runs with elevated authorization and thus should only be run on trusted hardware, e.g. an application server you control.

As I commented: there is no API to get a list of users.



来源:https://stackoverflow.com/questions/40869961/how-do-i-get-the-email-address-of-a-firebase-user-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!