如何根据用户ID在Firebase中获取用户电子邮件

Jor*_*ash 10 firebase

我需要获取用户对象,特别是用户电子邮件,我将使用以下格式的用户ID:

simplelogin:6

所以我需要写一个像这样的函数:

getUserEmail('simplelogin:6')

那可能吗?

Leo*_*sov 10

根据 Firebase 框架最新更新的当前解决方案:

firebase.auth().currentUser && firebase.auth().currentUser.email
Run Code Online (Sandbox Code Playgroud)

请参阅:https : //firebase.google.com/docs/reference/js/firebase.auth.Auth.html#currentuser

每个提供商都没有定义的电子邮件地址,但如果用户使用电子邮件进行身份验证。那么这将是实现上述解决方案的可能方法。

  • 这将如何通过给定的 ID 提供任何用户的电子邮件? (2认同)

Mar*_*ein 7

要获取当前登录用户的电子邮件地址,请使用 getAuth函数.对于电子邮件和密码/ simplelogin,您应该能够收到这样的电子邮件:

ref = new Firebase('https://YourFirebase.firebaseio.com');
email = ref.getAuth().password.email;
Run Code Online (Sandbox Code Playgroud)

在我看来,该password对象不是非常恰当地命名,因为它包含该email字段.

我认为通过uid获取任何用户的电子邮件地址不是Firebase功能.当然,这会将所有用户的电子邮件暴露给所有用户.如果您确实需要这个,则需要在创建帐户时通过他们的uid将每个用户的电子邮件保存到数据库中.然后,其他用户将能够通过uid从数据库中检索电子邮件.

  • 这是当前登录用户的电子邮件.不是您需要的任何用户的电子邮件. (7认同)

Qwe*_*rty 6

可以使用Admin SDK

Admin SDK不能在客户端上使用,只能在Firebase Cloud Functions中使用,然后可以从客户端调用。您将获得以下承诺:(设置云功能非常容易。)

admin.auth().getUser(uid)
admin.auth().getUserByEmail(email)
admin.auth().getUserByPhoneNumber(phoneNumber)
Run Code Online (Sandbox Code Playgroud)

在这里查看https://firebase.google.com/docs/auth/admin/manage-users#retrieve_user_data


简而言之,这就是您要寻找的

admin.auth().getUser(data.uid)
  .then(userRecord => resolve(userRecord.toJSON().email))
  .catch(error => reject({status: 'error', code: 500, error}))
Run Code Online (Sandbox Code Playgroud)

完整片段

在下面的代码中,我首先通过检查其uid是否在node下来验证调用此函数的用户是否有权显示有关任何人的此类敏感信息userRights/admin

export const getUser = functions.https.onCall((data, context) => {
  if (!context.auth) return {status: 'error', code: 401, message: 'Not signed in'}

  return new Promise((resolve, reject) => {
    // verify user's rights
    admin.database().ref('userRights/admin').child(context.auth.uid).once('value', snapshot => {
      if (snapshot.val() === true) {
        // query user data
        admin.auth().getUser(data.uid)
          .then(userRecord => {
            resolve(userRecord.toJSON()) // WARNING! Filter the json first, it contains password hash!
          })
          .catch(error => {
            console.error('Error fetching user data:', error)
            reject({status: 'error', code: 500, error})
          })
      } else {
        reject({status: 'error', code: 403, message: 'Forbidden'})
      }
    })
  })
})

Run Code Online (Sandbox Code Playgroud)

顺便说一句,在此处了解有关onCall()和之间的区别。onRequest()


Bha*_*ula 5

简单获取 firebaseauth 实例。我在 firebase 中创建了一个默认的电子邮件和密码。这只是为了安全,以便除了知道或购买我们产品的人使用我们的应用程序之外,没有人可以使用。下一步,我们将提供用于创建用户帐户的登录屏幕。

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String email = user.getEmail();
Run Code Online (Sandbox Code Playgroud)

每次用户打开应用程序时,如果当前用户不等于我们的默认电子邮件,则用户重定向到仪表板。下面是代码

mAuth = FirebaseAuth.getInstance();
    if (mAuth.getCurrentUser() != null){
        String EMAIL= mAuth.getCurrentUser().getEmail();
            if (!EMAIL.equals("example@gmail.com")){
                startActivity(new Intent(LoginActivity.this,MainActivity.class));
                finish();
            }
    }
Run Code Online (Sandbox Code Playgroud)

我也在寻找相同的解决方案,最后我得到了它。