问题描述
我可以获取Firebase函数(数据库触发器)的ID以便与Firebase规则一起使用吗?我只需要允许Firebase函数写入数据库.
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.
更新:
为澄清起见,我使用的是Trigger Database Firebase函数,并且我只希望将该函数写入数据库,但允许用户读取.我尝试了这种方法仅允许对Cloud Functions for Firebase进行写访问,但出现错误The default Firebase app already exists.
.但是,此链接适用于Cloud Functions(而非Firebase功能),并且我找不到任何其他相关问题.
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.
推荐答案
似乎对于Firebase和Google Cloud Functions的云功能存在误解(错误消息是因为在Cloud Function中使用了Google Cloud Functions代码对于Firebase),但是无论如何,这个问题的答案是,您可以在将代码写入Firebase数据库的函数的代码中使用adminRef代替ref:
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
代替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).
更新:似乎adminFef已由更改,因此请按照示例进行相应的替换.
Update: Seems that adminFef was replaced by ref due changes on Firebase, so follow the example to the respective replacement.
这篇关于将Firebase规则中的写入权限限制为云功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!