Cloud Functions 在更新或删除时收到 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())
最佳答案
在 event.data
内对象返回到您的 Cloud Function 的 onWrite()
回调,有两种类型的数据库引用:一种与触发写入的用户具有相同的权限(event.data.ref
),另一种是具有管理员权限,授予完全读写访问权限(event.data.adminRef
)。我无法判断,因为您没有提供显示您的 Cloud Function 的代码示例,但我敢打赌您使用的是 event.data.ref
。切换到使用 event.data.adminRef
应该可以解决您的问题。
关于javascript - 一些 Firebase 安全规则适用于 Cloud Functions 中的管理员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42737203/