In the example below, is there a way to get the uid of the user who wrote to /messages/{pushId}/original?
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const original = event.data.val();
console.log('Uppercasing', event.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return event.data.ref.parent.child('uppercase').set(uppercase);
});
Yes, this is technically possible, although it is not currently documented. The
uid
is stored with theevent.auth
object. When a Database Cloud Function is triggered from an admin situation (for example, from the Firebase Console data viewer or from an Admin SDK), the value ofevent.auth
is:When a Database Cloud Function is triggered from an unauthenticated reference, the value of
event.data
is:And finally, when a Database Cloud Function is triggered from an authed, but not admin, reference, the format of
event.auth
is:Given the information above, your best bet to get the
uid
of the user who triggered the event is to do the following:Just note that in the code above,
uid
would benull
even ifisAdmin
istrue
. Your exact code depends on your use case.WARNING: This is currently undocumented behavior, so I'll give my usual caveat of "undocumented features may be changed at any point in the future without notice and even in non-major releases."
Ever since Firebase functions reached version 1.0, this behavior is no longer undocumented but has sligtly changed. Be sure to read the docs.
Context has been added to cloud functions and you can use it like this