问题
Using a Firebase Realtime Database with these rules:
{
"rules": {
"messages": {
".read": "auth != null",
".write": "auth != null"
}
}
}
And, having this trigger defined as:
exports.localOnCreate = functions.database.ref('/messages/{messageId}')
.onCreate((snap, context) => {
console.log(context);
});
When I run this on the test website (authenticated via Google):
firebase.database().ref('/messages').push().set({
'hi': 'from-web'
}).then( function(result) {
console.log(result);
...
})
I see this in the Firebase Console Functions Log:
{
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'
}
}
If I am authenticated in the webapp, why am I getting authType: 'UNAUTHENTICATED', and auth: null, in the trigger "context" (second) argument?
And, as I see in CloudFunction reference, this function should receive only one argument, an Event object, but it seems it's not the case as seen in Realtime Database triggers, nor according to my tests. This last conflict got me completely confused. Any clarification will be greatly appreciated.
回答1:
Hey @RodrigoMata and @claytronicon, this was a bug. I created a github issue and they pushed out a new release v2.0.4 that contains a fix for authType from RTDB events. https://github.com/firebase/firebase-functions/pull/304
来源:https://stackoverflow.com/questions/51565780/firebase-oncreate-trigger-cant-get-authtype-other-than-unauthenticated