Cloud Function在更新或删除时会收到firebase权限被拒绝错误。
使用文件中的凭据初始化服务帐户,以便使用当前不可用于默认帐户的auth.createCustomToken
admin = require('firebase-admin');
functions = require('firebase-functions');
credential = admin.credential.cert(require('./credentials.json'));
admin.initializeApp({credential: credential, databaseURL: functions.config().firebase.databaseURL});
阻止更新的安全规则:
"downloads": {
"$key": {
".write": "data.val() == null"
}
}
用户将push
的数据插入/downloads
,然后Cloud Function尝试更新并随后删除。即使管理员帐户据称绕过所有安全规则(包括验证),这两个操作都会失败。
FIREBASE WARNING: update at /downloads/-Kexz33ljYjKblF_ZgUo failed: permission_denied
FIREBASE WARNING: set at /downloads/-Kexz33ljYjKblF_ZgUo failed: permission_denied
如果我将规则更改为:
,第一个错误(更新)将消失"downloads": {
"$key": {
".write": "newData.child('uid').val() == auth.uid"
}
}
解决方法是将event.data.ref
重新定义为
ref = admin.database().ref(event.data.ref.path.toString())
答案 0 :(得分:15)
在返回到您的Cloud Function的onWrite()
回调的event.data
对象中,有两种类型的数据库引用:一种限定为与触发写入的用户相同的权限({{ 3}})和一个具有管理员权限的范围,授予完全读写访问权限(event.data.ref
)。我无法分辨,因为您没有提供显示云功能的代码示例,但我敢打赌您使用的是event.data.ref
。切换为使用event.data.adminRef
可以解决您的问题。