Firebase规则不允许用户在android中写入数据
这是数据库结构 这是数据库规则:Firebase规则不允许用户在android中写入数据,firebase,firebase-realtime-database,firebase-security,Firebase,Firebase Realtime Database,Firebase Security,这是数据库结构 这是数据库规则: { "rules": { ".read": true, "shops-product-list": { "$uid": { // grants write access to the owner of this user account whose uid must exactly match the key ($uid) ".write": "auth !== null &&
{
"rules": {
".read": true,
"shops-product-list": {
"$uid": {
// grants write access to the owner of this user account whose uid must exactly match the key ($uid)
".write": "auth !== null && auth.uid === $uid",
}
}
}
}
商店产品列表的键记录在用户的uid中(忽略第一个键,即-1)
试图使用此逻辑写入新数据的已登录用户
private void addShopVariant(String pid, String vid, String mrp, String sellPrice) {
// Create new post at /user-posts/$userid/$postid and at
// /posts/$postid simultaneously
final String uid = getUid();
String key = pid + "_" + vid;
//String key = databaseReference.child("shops-product-list").child(uid).push().getKey();
ShopProductVariant shopProductVariant = new ShopProductVariant(pid, vid, mrp, sellPrice);
Map<String, Object> shopProductVariantValues = shopProductVariant.toMap();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/shops-product-list/" + uid + "/"+ key, shopProductVariantValues);
//childUpdates.put("/user-posts/" + userId + "/" + key, postValues);
databaseReference.updateChildren(childUpdates);
}
但我想只有登录的用户才能写,那也只有自己的数据了
以上firebase规则有什么问题?我认为您的规则设置错误: 应如下所示:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
注意它的语法!=不是==
编辑:
您可以这样做,而不是在服务器上验证:
scoresRef.child("the user id").setValue("your value", withCompletionBlock: { (error:NSError?, snapshot:FIRDatabaseReference) in
})
这样你就不必按照规则来做了我想这是只有请检查此官方文档链接。实际上,我正在使用此语法配置规则。我尝试了!=此外,但仍然存在权限被拒绝错误如果您尝试删除Firebase安全规则中的此部分“&&auth.uid====$uid”
==
和===
与的处理相同,您会得到什么=代码>和==代码>。快速浏览一下,这些规则似乎应该有效。您能否进一步减少代码,使其仅使用常量值?例如:getUid()
返回什么?那真的是登录用户吗?还请注意,updateChildren()
使用完成侦听器,这可能会告诉您有关操作失败原因的更多信息。
scoresRef.child("the user id").setValue("your value", withCompletionBlock: { (error:NSError?, snapshot:FIRDatabaseReference) in
})