Firebase的新手,并且阅读了文档,但我仍然不了解其中的几件事。我正在客户端建立一个管理面板来管理对用户的操作,因此该应用程序将没有一个注册页面,管理员将负责创建和激活用户。
我的理解是Firebase不会在“ Firestore”应用程序集合中存储用户数据,即使我创建了用户集合,它也不会自动向其中添加{email,displayName,password,phone,emailVerified,disabled}将存储在其他地方,我只能通过firebase-admin函数访问它。
如果是这种情况,那么最好的做法是为管理员创建一个HTTP函数,以创建用户赋予用户的角色,以及如何手动管理诸如{email,displayName,phone}之类的唯一字段,以及是否需要添加更多属性对于用户,我将需要创建一个配置文件集合,并将其与创建后的用户相关联。所以所有规则和验证都需要在函数中处理,是什么会触发admin.auth()。createUser(newUser)中的错误,什么是处理错误的最佳方法,我可以有一个全局错误处理程序,是否有一个具有最佳实践的项目在Firebase上获得想法的样板?
exports.createUser = functions.https.onCall(async (data, context) => {
if (!context.auth) {
createError('unauthenticated');
}
const roles = data.roles;
if (!roleIsValid(roles) || !userValid(data)) {
createError('dataInvalid');
}
const id= context.auth.uid;
const caller = await admin.auth().getUser(id);
if (!caller.customClaims.admin) {
createError('notAdmin');
}
const newUser = {
displayName: data.displayName,
password: data.password,
email: data.email,
emailVerified: true,
disabled: false
}
//todo: how to check all users if email, displayName, phone exists before creating user
const user = await admin.auth().createUser(newUser);
const userId = user.uid;
const claims = {};
roles.foreach(role => claims[role] = true);
await admin.auth().setCustomUserClaims(userId, claims);
await admin.firestore().collection("profile").doc(userId).set(data.profile);
return {message: 'User successfully created.'};
});
答案 0 :(得分:1)
在这里,我提到了一种通用模式,其中我们将Firebase auth.createUserWithEmailAndPassword()
用于注册。当我们使用 firebase auth 创建用户时,它会自动在 authentication 中存储基本信息(电子邮件,提供程序,创建者,已登录,用户UID)。但是这里我们将在 firestore 中维护一个 collection ,以存储带有一些附加字段的用户信息(假设我们的应用为 nickname )。而且,每当需要获取用户信息时,我们都会从 firestore 集合中获取信息,因为我们会将其保持在应用程序的最新状态。
auth.createUserWithEmailAndPassword
创建一个用户,我们知道它将在解决诺言时返回一个对象,该对象将包含user
,其中包含有关用户的基本信息。我们将定义一个函数来完成一些主要工作:
createUserProfileDocumentInFirestore(user, additionalData) {
const snapshot = firestore.doc(`users/${user.id}`).get();
if (!snapshot.exist) {
// set the user collection here with additional data
}
// it can possibly return the user document from here to show in our app
}
createUserProfileDocumentInFirestore
称为user
,我们得到了 1 和其他附加信息。