4
votes

When one of my users creates a specific document, for example a post, I would like to add its UID to the document.

I tried to find a way to achieve that through cloud functions and the onCreate trigger, but it seems that the user's UID isn't reachable within those function.

exports.createPublication = functions.firestore
    .document('publications/{publication}')
    .onCreate((snap, context) => {

      const newValue = snap.data();

      return snapshot.ref.child('authorId').set("ID");

    });

I was really suprised that Firestore doesn't add natively a AuthorId field like others databases would do (Mongo, Parse, etc...) Of course, I can add this UID from the client, but I think it is a bit dirty.

1

1 Answers

4
votes

Firestore triggers currently have no way to know the uid for the authenticated user that made the change that invoked the trigger. This is a feature that's being looked into.

If the document ID happens to be the uid of the user, you could use security rules to restrict write access to that user, then use wildcards to know the uid, but it sounds like that trick can't be used here.