在Firebase的Cloud Functions中从数据库中获取用户标识? [英] Getting the user id from a database trigger in Cloud Functions for Firebase?

查看:127
本文介绍了在Firebase的Cloud Functions中从数据库中获取用户标识?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的例子中,是否有一种方法可以获得写给/ messages / {pushId} / original的用户的uid?

  exports.makeUppercase = functions.database.ref('/ messages / {pushId} / original')
.onWrite(event => {
//获取当前的值写入实时数据库
const original = event.data.val();
console.log('Uppercasing',event.params.pushId,original);
const uppercase = original.toUpperCase();
//当在
函数中执行异步任务时,必须返回一个Promise
//设置一个大写在实时数据库中的兄弟会返回一个Promise。
return event.data.ref.parent.child('uppercase').set(uppercase);
});


解决方案

是的,这在技术上是可行的,目前记录。 uid 存储在 event.auth 对象中。从管理员情况(例如,从Firebase控制台数据查看器或Admin SDK)触发数据库云端功能时, event.auth 的值为:

  {
admin:true
}

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

  {
admin:false
}


$ b 最后,当一个数据库云端函数从一个authed而非admin的引用触发时, event.auth的格式是:

  {
admin:false,
variable :{
provider:< PROVIDER>,
provider_id:< PROVIDER>,
user_id:< UID& $ btoken:{
//解码的授权令牌声明,例如sub,aud,iat,exp等
},
uid:< UID>






$ b给出上面的信息,触发该事件的用户的 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 即使 isAdmin true ,也会是 null 。您的确切代码取决于您的用例。



警告:这是目前没有记录的行为,所以我会给我通常的警告无证功能可能会在未来的任何时候改变,恕不另行通知,甚至在非主要版本。


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 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."

这篇关于在Firebase的Cloud Functions中从数据库中获取用户标识?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆