【发布时间】:2019-01-05 00:58:06
【问题描述】:
使用符合以下规则的 Firebase 实时数据库:
{
"rules": {
"messages": {
".read": "auth != null",
".write": "auth != null"
}
}
}
并且,将此触发器定义为:
exports.localOnCreate = functions.database.ref('/messages/{messageId}')
.onCreate((snap, context) => {
console.log(context);
});
当我在测试网站上运行它时(通过 Google 验证):
firebase.database().ref('/messages').push().set({
'hi': 'from-web'
}).then( function(result) {
console.log(result);
...
})
我在 Firebase 控制台功能日志中看到了这一点:
{
eventId: '............',
timestamp: '2018-07-27T20:51:50.943Z',
eventType: 'google.firebase.database.ref.create',
resource: {
service: 'firebaseio.com',
name: 'projects/_/instances/..../refs/messages/-LISVklGLfbcfrFvnylZ'
},
authType: 'UNAUTHENTICATED',
auth: null,
params: {
messageId: '-LISVklGLfbcfrFvnylZ'
}
}
如果我在 webapp 中通过了身份验证,为什么我会在触发器“上下文”(第二个)参数中得到 authType: 'UNAUTHENTICATED', 和 auth: null, ?
而且,正如我在CloudFunction reference 中看到的,这个函数应该只接收一个参数,一个Event 对象,但似乎不是Realtime Database triggers 中看到的情况,也不是根据我的测试。最后的冲突让我完全糊涂了。任何澄清将不胜感激。
【问题讨论】:
-
您可以改为通过您的应用手动调用您的函数,这样您就可以将 auth 对象传递给它。 See the documentation
-
我遇到了确切的问题。我会让你知道我发现了什么。 fwiw > v1 有两个参数。请参阅: Before ( { const createdData = event.data.val(); // 数据创建 }); Now (>= v1.0.0) exports.dbCreate = functions.database.ref('/path').onCreate((snap, context) => { const createdData = snap.val(); // 创建的数据 } );firebase.google.com/docs/functions/beta-v1-diff
-
我尝试在客户端使用电子邮件/密码和自定义令牌进行身份验证,结果相同。
-
@RodrigoMata 谢谢,但我宁愿让事情尽可能整洁,在这个特殊问题中,我宁愿解决它而不是绕过它。我正在尝试在数据库的某些部分中记录更改(更改时间和更改内容,由谁更改;类似于审核日志),因此 web/https 可调用不是我的选择。还是谢谢!
-
@claytronicon 感谢您的澄清和测试。
标签: firebase firebase-realtime-database google-cloud-functions