0

Currently, I'm using the email as the document name but I'd like to use the UID instead. I tried using "firebase.auth().currentUser.uid" but that retrieves the UID from the last user.

try {
      setError("");
      setLoading(true);
      await signup(emailRef.current.value, passwordRef.current.value);
      await db
        .collection("users")
        .doc(emailRef)
        .set(
          {
            firstName: firstNameRef.current.value,
            lastName: lastNameRef.current.value,
          },
          { merge: true }
        )
  • Hello, can you please share your complete code so we can see the function `signup()` and var like `emailref` ? – Dharmaraj Apr 06 '21 at 16:36

1 Answers1

1

I'm not sure about the function signup() and variables emailRef but you can try this. You can get the UID of newly created user from userCredential object.

firebase.auth().createUserWithEmailAndPassword(email, password)
  .then(async (userCredential) => {
    // Signed in 
    const user = userCredential.user;
    await db.collection("users").doc(user.uid).set({
      firstName: firstNameRef.current.value,
      lastName: lastNameRef.current.value,
    }, { merge: true })
    // ...
  })
  .catch((error) => {
    var errorCode = error.code;
    var errorMessage = error.message;
    // ..
  });
Dharmaraj
  • 47,845
  • 8
  • 52
  • 84