35

在下面的示例中,有没有办法获取写入 /messages/{pushId}/original 的用户的 uid?

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);
});
4

2 回答 2

50

UPDATED ANSWER (v1.0.0+):

As noted in @Bery's answer above, version 1.0.0 of the Firebase Functions SDK introduced a new context.auth object which contains the authentication state such as uid. See "New properties for user auth information" for more details.

ORIGINAL ANSWER (pre v1.0.0):

Yes, this is technically possible, although it is not currently documented. The uid is stored with the event.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 of event.auth is:

{
  "admin": true
}

When a Database Cloud Function is triggered from an unauthenticated reference, the value of event.data is:

{
  "admin": false
}

And finally, when a Database Cloud Function is triggered from an authed, but not admin, reference, the format of event.auth is:

{
  "admin": false,
  "variable": {
    "provider": "<PROVIDER>",
    "provider_id": "<PROVIDER>",
    "user_id": "<UID>",
    "token": {
      // Decoded auth token claims such as sub, aud, iat, exp, etc.
    },
    "uid": "<UID>"
  }
}

Given the information above, your best bet to get the uid of the user who triggered the event is to do the following:

exports.someFunction = functions.database.ref('/some/path')
  .onWrite(event => {
    var isAdmin = event.auth.admin;
    var uid = event.auth.variable ? event.auth.variable.uid : null;

    // ...
});

Just note that in the code above, uid would be null even if isAdmin is true. 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."

于 2017-03-12T17:42:39.080 回答
23

自从 Firebase 函数达到 1.0 版以来,这种行为不再是无证的,而是发生了轻微的变化。请务必阅读文档

上下文已添加到云功能中,您可以像这样使用它

  exports.dbWrite = functions.database.ref('/path/with/{id}').onWrite((data, context) => {
  const authVar = context.auth; // Auth information for the user.
  const authType = context.authType; // Permissions level for the user.
  const pathId = context.params.id; // The ID in the Path.
  const eventId = context.eventId; // A unique event ID.
  const timestamp = context.timestamp; // The timestamp at which the event happened.
  const eventType = context.eventType; // The type of the event that triggered this function.
  const resource = context.resource; // The resource which triggered the event.
  // ...
});
于 2018-04-12T12:19:47.453 回答