0

I've a very simple goal to build an auditory trigger for firestore.
I would like to save who created, when, who last updated and when for some of my documents.

I've searched up and down on all firestore documentation and it seems to be an impossible task

exports.testTrigger = functions.runWith({
        timeoutSeconds: 10,
        memory: '128MB'
    }).firestore
    .document('/environment/{prod}/companies/{company}/referrals/{ref}')
    .onWrite(async (change, context) => {
            return change.after.ref.set({
                update_by: context.auth.uid
            }, {
                merge: true
            });
        });

context.auth is undefined even though the documentation say it should exist

I found this SO question which the first answer says is impossible
https://firebase.google.com/docs/reference/functions/firebase-functions.eventcontext.md#eventcontext_interface

the quality of googles documentation is really sick

Getting the user id from a Firestore Trigger in Cloud Functions for Firebase?

does anyone have any other ideas?

Rafael Lima
  • 3,079
  • 3
  • 41
  • 105
  • 2
    As stated by the [documentation you linked](https://firebase.google.com/docs/reference/functions/firebase-functions.eventcontext.md#eventcontextauth) - "This field is only populated for Realtime Database triggers and Callable functions. For an unauthenticated user, this field is null. For Firebase admin users and event types that do not provide user information, this field does not exist." – samthecodingman Apr 14 '23 at 10:18
  • The current practice is to mandate that the updating user attaches their user id in the data they are updating as [covered here](https://stackoverflow.com/a/50842161/3068190). The Firestore rule would include a `request.resource.data.update_by == request.auth.uid` condition. – samthecodingman Apr 14 '23 at 10:22

0 Answers0