问题
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