Javascript 在Firebase的云函数中从数据库触发器获取用户id?

Javascript 在Firebase的云函数中从数据库触发器获取用户id?,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,在下面的示例中,是否有方法获取写入/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 = even

在下面的示例中,是否有方法获取写入/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);
});
更新答案(v1.0.0+)

如中所述,Firebase Functions SDK的版本
1.0.0
引入了一个新对象,其中包含身份验证状态,如
uid
。有关更多详细信息,请参阅

原始答案(v1.0.0之前):

是的,这在技术上是可行的,尽管目前还没有记录在案。
uid
event.auth
对象一起存储。当从管理员情况(例如,从Firebase Console data viewer或admin SDK)触发数据库云函数时,
event.auth
的值为:

{
  "admin": true
}
{
  "admin": false
}
{
  "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>"
  }
}
当从未经验证的引用触发数据库云函数时,
event.data
的值为:

{
  "admin": true
}
{
  "admin": false
}
{
  "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>"
  }
}
最后,当一个数据库云函数是从一个经过身份验证的引用(而不是管理员引用)触发时,
event.auth
的格式是:

{
  "admin": true
}
{
  "admin": false
}
{
  "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>"
  }
}
请注意,在上面的代码中,
uid
将为
null
,即使
isAdmin
true
。您的确切代码取决于您的用例


警告:这是当前未记录的行为,因此我将给出我通常的警告:“未记录的功能可能会在未来的任何时候更改,恕不另行通知,甚至在非主要版本中也会更改。”

自从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.
  // ...
});

有什么原因没有一个公开的API?似乎是一个合理的用例(我们正在探索FB作为后端,这是一个主要的未决问题)。有迹象表明这将改变或保持吗?这是我的用例的理想选择,但如果仍然可以更改,我不想使用它。添加了一个bug/请求来记录这一点,并将其作为正式的公共API,以防其他人在event.auth不是来自admin时怀疑这是整个对象:看起来语法随着v1.0.0的发布而发生了更改,现在记录如下: