5

Can I get an ID for a Firebase function (a database trigger) to use with Firebase Rules? I need to allow only a Firebase function to write to the database.

Update:

To clarify, I am using a Trigger Database Firebase function, and I want to allow only that function to write to the database, but allow users to read. I tried this approach Allowing write access only to Cloud Functions for Firebase but get an error The default Firebase app already exists.. However this link is for Cloud Functions and not Firebase functions, and I couldn't find any other related questions.

will-hart
  • 3,742
  • 2
  • 38
  • 48
moonw
  • 372
  • 2
  • 15
  • When you say *id* do you mean the parent node key? Or users uid? Or something else? – Jay Apr 26 '17 at 21:38
  • Hi Jay, I mean the ID of the function (if exist). I have added the answer below, there is no need to check a function ID, just using the adminRef instead ref, I mean, if you uses the 'ref' you can deny on the rules to everybody and the Function can not write, but if is used 'adminRef' only your function will write or read. Very useful. – moonw Apr 27 '17 at 13:30

1 Answers1

4

Seems that there is a misunderstood about Cloud Functions for Firebase and Google Cloud Functions (the error message is because is used code for a Google Cloud Functions within a Cloud Function for Firebase), but anyway the answer of this question is that you can use adminRef instead ref in the code of the function that will write the data on Firebase database:

event.data.adminRef instead event.data.ref

this will give you full read and write access instead of end-user access (please note that also you must take care to indicate within the rules deny to everybody).

Update: Seems that adminFef was replaced by ref due changes on Firebase, so follow the example to the respective replacement.

moonw
  • 372
  • 2
  • 15