1

Is it possible to get the uid of the user who triggered a cloud function through the context.auth property when using firestore?

exports.updateReport = functions.firestore
.document('groups/{groupId}/reports/{nursingId}')
.onUpdate((change, context) => {
    const uid : string | undefined = context.auth?.uid;
})

I found a lot of old questions concerning the same problem with many different responses. (the feature works, the feature will maybe sometime work or that the feature will never work)

In my local setup I context.auth is always undefined so I don't know if I am making a mistake or it doesn't even work at all. And if that is the case, is there any other secure way to get the uid of the user, who triggered the corresponding cloud function?

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121
Max Gusenbauer
  • 217
  • 2
  • 12

1 Answers1

5

No, this is not possible with Firestore Cloud Functions: For these Cloud Functions the context parameter does not contain the authentication information for the user that triggered the function.

Note that it is possible with Realtime Database Cloud Functions: they provide authentication information for the user that triggered the function via the context parameter.


One workaround is to have a field in the Firestore doc that contains the user uid.

If you use this workaround, depending on your exact use case (you wrote "is there any other secure way" in your question), you may write a security rule validation that checks that this field does contain the user's uid (e.g. request.resource.data.userId == request.auth.uid).

Renaud Tarnec
  • 79,263
  • 10
  • 95
  • 121