Search code examples
google-cloud-firestorefirebase-authentication

How do I get the user id of the person who created a document in Firestore?


I added authentication from firebase to my client app and am using firestore for the client to create posts.

        const docRef = await addDoc(collection(db, "posts"), {
          content: "foo"
        });

But when I check in firestore, I don't see any associated user to a post. How do I see this? I could probably add a uid field but it's not a guarantee that all docs will be associated to a user.


Solution

  • Firestore doesn't record anything related to the authenticated user by default. You should store the user's UID in a field, if that's what you want (this is an extremely common practice). The only thing that Firestore uses authentication for is the evaluation of security rules, which is typically used for controlling per-user access to documents.

    It sounds like you'll want to store that field conditionally if there is not always an authenticated user performing the update.