管理员(教练)如何通过firebase云功能创建新用户(运动员)?



我正在为教练和运动员构建一个web应用程序。在应用程序中,教练应该能够将运动员添加到应用程序中。这些运动员应该会收到一封带有登录链接的电子邮件,并且该运动员应该被添加到firestore数据库中的用户集合中。

我创建了以下云函数,检查用户是否是教练,然后创建一个新用户。

exports.createUser = functions.https.onCall((data, context) => {
if (!context.auth?.token.coach) return "You are not a coach!";
return admin
.auth()
.createUser({
email: data.email,
emailVerified: true,
password: data.password,
displayName: data.firstname,
})
.then((userRecord) => {
console.log("Successfully crated new user:", userRecord.uid);
return userRecord.uid;
})
.catch((error) => {
console.log("Error creating new user:", error);
return "An error has occured";
});
});

然后在本节中调用cloud函数。

const functions = getFunctions();
const addGymnasts = httpsCallable(functions, "createUser");
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const ref = collection(db, "Gymnasts", user.uid, "Gymnasts");
await addDoc(ref, {
firstname: firstname,
lastname: lastname,
email: email,
uid: user.uid,
createdAt: serverTimestamp(),
});
await addGymnasts({ email: "", firstname: "", password: ""});
setFirstname("");
setLastname("");
};

新用户被存储在正确集合中的firestore数据库中,并且它自己运行云函数,但是新创建的用户没有添加到Authentication中的Users中。什么是不正确的?

const functions = getFunctions();
const addGymnasts = httpsCallable(functions, "createUser");
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const ref = collection(db, "Gymnasts", user.uid, "Gymnasts");
await addDoc(ref, {
firstname: firstname,
lastname: lastname,
email: email,
uid: user.uid,
createdAt: serverTimestamp(),
});
addGymnasts(); // Here i've put the cloud function 
setFirstname("");
setLastname("");
};

相关内容

  • 没有找到相关文章