【问题标题】:Getting the user id from a database trigger in Cloud Functions for Firebase?从 Cloud Functions for Firebase 中的数据库触发器获取用户 ID?
【发布时间】:2018-05-21 12:02:14
【问题描述】:

在下面的示例中,有没有办法获取写信给 /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);
});

【问题讨论】:

    标签: javascript firebase firebase-realtime-database google-cloud-functions


    【解决方案1】:

    更新答案 (v1.0.0+)

    @Bery's answer above 中所述,Firebase Functions SDK 的版本 1.0.0 引入了一个新的 context.auth 对象,其中包含身份验证状态,例如 uid。详情请见"New properties for user auth information"

    原始答案(pre v1.0.0):

    是的,这在技术上是可行的,尽管目前没有记录。 uidevent.auth 对象一起存储。当从管理员情况(例如,从 Firebase 控制台数据查看器或从 Admin SDK)触发数据库云函数时,event.auth 的值是:

    {
      "admin": true
    }
    

    当未经身份验证的引用触发数据库云函数时,event.data 的值为:

    {
      "admin": false
    }
    

    最后,当数据库云函数从经过身份验证但不是管理员的引用触发时,event.auth 的格式为:

    {
      "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 的最佳选择是执行以下操作:

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

    请注意,在上面的代码中,uid 将是 null,即使 isAdmintrue。您的确切代码取决于您的用例。

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

    【讨论】:

    • 为什么没有公开的 API?似乎是一个合理的用例(我们正在探索 FB 作为后端,这是一个主要的悬而未决的问题)。
    • 有任何迹象表明这种情况会改变或保持不变?这非常适合我的用例,但如果仍然可以更改,我不想使用它。
    • 添加了一个错误/请求来记录这一点并使其成为官方公共 API github.com/firebase/firebase-functions/issues/133
    • 如果其他人想知道当 event.auth 不是来自管理员时这是整个对象:pastebin.com/CgyivDRm
    • 看起来语法随着 v1.0.0 的发布而改变,现在记录在案:firebase.google.com/docs/functions/beta-v1-diff
    【解决方案2】:

    自从 Firebase 函数达到 1.0 版后,这种行为就不再是无证的,而是略有改变。请务必阅读the docs

    已将上下文添加到云函数中,您可以这样使用它

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

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2017-10-02
      相关资源
      最近更新 更多