Home > Net >  Firebase/javascript -- How to connect data created in firestore with user
Firebase/javascript -- How to connect data created in firestore with user

Time:11-03

Firebase v9

How to connect data created in firestore with user who created this data?

I use the createUserWithEmailAndPassword function to authorize user in firebase and after authorization I getting user data with onAuthStateChanged, but how could i connect particular user with data that is created only by them?

CodePudding user response:

To create for example a profile document for a specific user, the common pattern is to use the user's UID as the document ID. Something like this:

import { getAuth, onAuthStateChanged } from "firebase/auth";
import { doc, setDoc } from "firebase/firestore"; 

const auth = getAuth();
onAuthStateChanged(auth, (user) => {
  if (user) {
    const uid = user.uid;

    await setDoc(doc(db, "users", uid), {
      name: user.displayName
    });
    // ...
  }
});

Also see:

  • Related