【发布时间】:2015-07-05 13:13:46
【问题描述】:
我需要获取一个用户对象,特别是用户电子邮件,我将获得这种格式的用户 ID:
simplelogin:6
所以我需要写一个类似这样的函数:
getUserEmail('simplelogin:6')
这可能吗?
【问题讨论】:
-
这是旧的但没有好的答案。你有没有自己解决过?电话号码是否相似(自提出此问题以来的新功能)
标签: firebase
我需要获取一个用户对象,特别是用户电子邮件,我将获得这种格式的用户 ID:
simplelogin:6
所以我需要写一个类似这样的函数:
getUserEmail('simplelogin:6')
这可能吗?
【问题讨论】:
标签: firebase
Admin SDK 不能在客户端上使用,只能在您可以从客户端调用的 Firebase Cloud Functions 中使用。您将获得这些承诺:(set a cloud function up 真的很容易。)
admin.auth().getUser(uid)
admin.auth().getUserByEmail(email)
admin.auth().getUserByPhoneNumber(phoneNumber)
请看这里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}))
在下面的代码中,我首先通过检查他的uid是否在节点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'})
}
})
})
})
顺便说一句,了解onCall() 和onRequest() here 之间的区别。
【讨论】:
根据 Firebase 框架的最新更新的当前解决方案:
firebase.auth().currentUser && firebase.auth().currentUser.email
见:https://firebase.google.com/docs/reference/js/firebase.auth.Auth.html#currentuser
每个提供商都没有定义的电子邮件地址,但如果用户使用电子邮件进行身份验证。那么这将是实现上述解决方案的一种可能方式。
【讨论】:
要获取当前登录用户的电子邮件地址,请使用getAuth 函数。对于电子邮件和密码/simplelogin,您应该能够收到这样的电子邮件:
ref = new Firebase('https://YourFirebase.firebaseio.com');
email = ref.getAuth().password.email;
在我看来,password 对象的命名不是很贴切,因为它包含 email 字段。
我相信通过 uid 获取任何用户的电子邮件地址并不是 Firebase 的一项功能。当然,这会将所有用户的电子邮件暴露给所有用户。如果您确实想要这样做,您需要在创建帐户时通过他们的 uid 将每个用户的电子邮件保存到数据库中。然后其他用户将能够通过 uid 从数据库中检索电子邮件。
【讨论】:
简单地获取 firebaseauth 实例。 我在 firebase 中创建了一个默认电子邮件和密码。这只是为了安全起见,除了谁知道或谁购买了我们的产品来使用我们的应用程序之外,没有人可以使用。 下一步,我们将为用户帐户创建提供登录屏幕。
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String email = user.getEmail();
每次用户打开应用程序时,如果当前用户不等于我们的默认电子邮件,用户就会重定向到仪表板。 下面是代码
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();
}
}
我也在寻找相同的解决方案,终于找到了。
【讨论】:
当前解决方案(Xcode 11.0)
Auth.auth().currentUser? ?? "Mail"
Auth.auth().currentUser?.email ?? "User"
【讨论】:
我遇到了同样的问题。需要将 Firestore 中的 email 替换为 uid,以免电子邮件到处存在。 可以使用服务帐户从您计算机上的脚本调用它。您不需要 Firebase 函数。
首先生成服务帐户并下载其 json 密钥。
Firebase 控制台 > 齿轮图标 > 项目设置 > 服务帐户 > 生成新的私钥按钮。
https://console.firebase.google.com/u/0/project/MYPROJECT/settings/serviceaccounts/adminsdk
然后创建项目,添加密钥并调用 Admin SDK。
npm initnpm install dotenv firebase-admin.keys 目录,保持项目目录中没有密钥文件。还有.gitignore目录。.env 文件,如下所示:GOOGLE_APPLICATION_CREDENTIALS=".keys/MYPROJECT-firebase-adminsdk-asdf-234lkjjfsoi.json"。我们将使用dotenv 稍后加载它。index.js:const admin = require('firebase-admin');
admin.initializeApp({
credential: admin.credential.applicationDefault(),
});
(async () => {
const email = "admin@example.com";
const auth = admin.auth();
const user = await auth.getUserByEmail(email);
// Or by uid as asked
//const user = await auth.getUser(uid);
console.log(user.uid, user.email);
//const firestore = admin.firestore();
// Here be dragons...
})();
node -r dotenv/config index.js
【讨论】: