firebasegoogle-cloud-functions

"event.auth" equivalent in 2nd gen Firebase cloud functions?


I am trying to retrieve the uid (on server side) whenever a realtime database action is performed. I saw in the 1st Gen Firebase cloud functions, you can do it by accessing context.auth.uid as shown in the docs. But on the 2nd Gen, the docs didn't say. How should I do it?


Solution

  • firebaser here

    The 1st generation of Cloud Functions for Firebase indeed had a context.auth parameter available for Realtime Database triggers that specifies the current user in the app at the time the database write was made.

    There is currently no equivalent to this context.auth in the 2nd generation of Cloud Functions, due to the changes in the underlying architecture. So for the moment, you'll have to work around this in the same way you always had to for other trigger types: by passing the UID as part of the context/path, and ensuring that users can only write to their own UID through security rules. It's not terribly difficult, but a nuisance indeed given that this used to be built-in functionality.

    There is internal interest to bring it back, so I recommend filing a feature request for it on firebase.uservoice.com and with the Firebase support team.


    Update: Early 2024 the auth context was added to Cloud Firestore event triggers for gen 2 Cloud Functions, which you can read more about here. So while the information still isn't available for Realtime Database triggers, this shows that it is at least feasible in the system.