如何通过ID获取Firebase用户的电子邮件地址 [英] How do I get the email address of a Firebase user by ID

查看:81
本文介绍了如何通过ID获取Firebase用户的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Firebase中使用电子邮件/密码身份验证.因此,每个用户都有一个电子邮件地址.我的用户位于组中,我希望每个用户都可以看到该组中的所有其他用户.具体来说,我想在前端向用户显示电子邮件列表.我的问题不是关于如何为此制定规则,而是如何给定用户ID的用户的电子邮件地址?请注意,我要询问的是获取其他用户的列表,而不是当前登录的用户的列表.

我还没有找到任何SO答案来说明如何通过 auth()获取另一个用户的电子邮件地址.我已经看到建议将电子邮件放在用户ID下的/users 集合中的建议,但是对于我将电子邮件地址同时存储在 auth()中来说,这似乎太脆弱了和/users/$ userId .更改电子邮件地址将是一场噩梦.所以必须有一种方法可以从 auth()获取电子邮件,对吗?

谢谢!

解决方案

如果您要检索特定用户的电子邮件地址,则可以使用新的Firebase Admin SDK.

  admin.auth().getUser(uid).then(function(userRecord){//有关userRecord的内容,请参见下表console.log(成功获取用户数据:",userRecord.toJSON());}).catch(function(error){console.log(获取用户数据时出错:",错误);}) 

请参见有关检索用户数据的文档./p>

顾名思义,Admin SDK在提升的授权下运行,因此应仅在受信任的硬件(例如您控制的应用服务器.

正如我评论的那样:没有API可以获取用户列表.

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!

解决方案

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.

这篇关于如何通过ID获取Firebase用户的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆