0

How can determine the authenticated user who made the change within a Firestore 2nd gen trigger?

The 1st gen onUpdate trigger provides a change object & a context object where you can get the auth. The equivalent 2nd gen onDocumentUpdated instead only provides an event object which doesn't appear to include the auth user.

1st gen

export const onUserUpdate = Firestore
    .document("users/{userId}")
    .onUpdate(async (change, context) => {
        const before = change.data.before.data();
        const after = change.data.after.data();

        // Access the authenticated user information
        const user = context.auth;

        if (user) {
            console.log("Authenticated user ID:", user.uid);
            console.log("Authenticated user email:", user.token.email);
            console.log("Authenticated user name:", user.token.name);
            // You can access more user properties as needed
        } else {
            console.log("No authenticated user information available.");
        }
});

2nd gen

exports.onUserUpdate = onDocumentUpdated("user/{userId}", async (event) => {
    const before = event.data.before.data();
    const after = event.data.after.data();

    // how do I get auth?

    
});

event: FirestoreEvent<Change<QueryDocumentSnapshot>

https://firebase.google.com/docs/functions/firestore-events?gen=2nd

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
ctown4life
  • 835
  • 1
  • 11
  • 24

1 Answers1

2

The 1st gen onUpdate trigger provides a change object & a context object where you can get the auth.

Actually, you can't get the user from that v1 context. See:

And you still can't with v2 triggers.

You are welcome to send your feedback to Firebase support about this, but I doubt anything will change soon as it's been over 5 years of people asking for this.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441