New to Firebase and having read the documentation I still don't understand a couple of the things. Am building an administrative panel on client-side to manage crud operations on users and the app will not have a sign-up page admin handles the creation and activating users.
My understanding is Firebase does not store user data in "Firestore" the apps collection even if I create a user collection it will not automatically add the {email, displayName, password, phone, emailVerified, disabled} to it instead this data will be stored somewhere else and I can only access it by firebase-admin function.
If this is the case then is best practice would be to create an HTTP function for admin to create users give them roles and some how manually manage unique fields like {email, displayName, phone} and if I need to add more properties to a user I will need to create a profile collection and associate it to a user after creation. so all rules and validation need to be handled in the function what can trigger an error in admin.auth().createUser(newUser) whats best method to handle errors, can I have a global error handler, is there a project with best practices a boilerplate to get an idea on 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.'};
});