首页 > 解决方案 > 在 Firebase Android 中读写

问题描述

我正在使用具有以下规则的 Firebase 实时数据库

{
    "rules": {
      "users": {
        "$uid": {
          ".read": "$uid === auth.uid",
          ".write": "$uid === auth.uid"
         }
       }
    }
}

我在数据库中编写的代码是

mDatabase = FirebaseDatabase.getInstance().getReference("OWNER_DATA");
String id = mDatabase.push().getKey();
            OwnerSignup ownerSignup = new OwnerSignup(id, name, email, phoneNumber);
            mDatabase.child("MY_DATA").setValue(ownerSignup);

如果我将规则更改为

read:"false" 
write:"false"

我可以推送数据,但使用上述规则,我无法在数据中插入值。请帮我解决这个问题。

以及如何在我的 API 为“ https://rent-c-246c0.firebaseio.com/ ”的 Postman 中使用 API 调用它

我尝试在邮递员中使用以下给定的 API https://rent-c-246c0.firebaseio.com/OWNER_DATA.json

在这里我想传递我的 auth.uid 令牌

标签: androidfirebasefirebase-realtime-databasefirebase-security

解决方案


如果您使用以下规则:

read:"false" and write:"false"

这意味着您无法检索或向数据库添加数据。

如果您使用:

{
  "rules": {
    "some_path": {
      "$uid": {
        // Allow only authenticated content owners access to their data
        ".read": "request.auth.uid == uid"
        ".write": "request.auth.uid == uid"
      }
    }
  }
}

然后,这些规则将访问权限仅限于经过身份验证的内容所有者。数据只能由一个用户读写,数据路径包含用户的ID。

https://firebase.google.com/docs/rules/basics#content-owner_only_access


推荐阅读