使用具有以下规则的 Firebase 实时数据库:
{
"rules": {
"messages": {
".read": "auth != null",
".write": "auth != null"
}
}
}
并且,将此触发器定义为:
exports.localOnCreate = functions.database.ref('/messages/{messageId}')
.onCreate((snap, context) => {
console.log(context);
});
当我在测试网站上运行它时(通过谷歌验证):
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'
}
}
如果我在 web 应用程序中进行了身份验证,为什么我会收到authType: '未经身份验证', and 授权:空,在触发器“上下文”(第二个)参数中?
而且,正如我所见云函数参考 https://firebase.google.com/docs/reference/functions/functions#.CloudFunction,这个函数应该只接收一个参数,一个 Event 对象,但似乎情况并非如此实时数据库触发器 https://firebase.google.com/docs/functions/database-events,也不根据我的测试。最后的冲突让我完全困惑。任何澄清将不胜感激。