【问题标题】:Write and Read in Firebase Android在 Firebase Android 中读写
【发布时间】:2020-01-22 08:53:41
【问题描述】:

我正在使用具有以下规则的 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 所在的 Postman 中使用 API 调用它 "https://rent-c-246c0.firebaseio.com/"

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

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

【问题讨论】:

    标签: android firebase firebase-realtime-database firebase-security


    【解决方案1】:

    如果您使用以下规则:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-12-04
      • 2019-03-22
      • 2016-03-10
      • 2020-12-20
      • 1970-01-01
      • 1970-01-01
      • 2016-01-01
      • 2020-07-08
      相关资源
      最近更新 更多