管理员(教练)如何通过firebase云函数创建一个新用户(运动员)?

2cmtqfgy  于 2023-01-05  发布在  其他
关注(0)|答案(1)|浏览(151)

我正在为教练和运动员开发一个网络应用程序,在这个应用程序中,教练应该能够将运动员添加到应用程序中,然后这些运动员会收到一封带有登录链接的电子邮件,并且这个运动员会被添加到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(),
    });
    addGymnasts(); // Here i've put the cloud function 
    setFirstname("");
    setLastname("");
  };

新用户存储在firestore数据库中的正确集合中,但新创建的用户未添加到身份验证中的用户。

62lalag4

62lalag41#

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("");
  };

相关问题