【问题标题】:Can't add in Firebase when authentified Android验证 Android 时无法添加 Firebase
【发布时间】:2017-12-21 11:05:08
【问题描述】:

通过身份验证后,我无法在 firebase 中添加评论。

这是我的规则。

{
    "rules": {
        ".read": true,
        ".write": "auth != null"
    }
}

我在推入 firebase 之前测试用户是否不为空的活动。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String baseUrl = "url";
        final Firebase fb = new Firebase(baseUrl+"/comments");
        final EditText et = (EditText) findViewById(R.id.comment);
        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        Button btn = (Button) findViewById(R.id.send);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(user != null)
                    fb.push().child("comment").setValue(et.getText().toString());
            }
        });
    }

}

错误 我进入日志

W/RepoOperation: setValue at /cmets/-L0shVMfCJWab7_5ltnO/comment failed: FirebaseError: Permission denied

当我将写入规则更改为 true 时,它​​起作用了。

【问题讨论】:

    标签: java android firebase firebase-authentication


    【解决方案1】:

    我解决了问题,改为使用 Firebase 实例我使用 DatabaseReference。

    这里是代码

    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String baseUrl = "https://tpdevproject.firebaseio.com/";
        final DatabaseReference fb = FirebaseDatabase.getInstance().getReference("comments");
        final EditText et = (EditText) findViewById(R.id.comment);
        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        Button btn = (Button) findViewById(R.id.send);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(user != null)
                        fb.push().child("comment").setValue(et.getText().toString());
                }
            });
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      您需要在 firebase 控制台中指定两个规则都为真-:

      {
          "rules": {
              ".read": true,
              ".write": true,
          }
      }
      

      【讨论】:

      • 如果你没有设置为true,你需要指定如何将值写入firebase。指定true意味着你有写入权限
      • 我的意思是在没有身份验证的情况下读取数据并在用户经过身份验证后写入数据(为了提高安全性)。
      【解决方案3】:

      试试这个:

      {
          "rules": {
              ".read": true,
              ".write": "auth !== null"
          }
      }
      

      注意写规则中的!==,用两个等号代替一个。

      此答案基于Firebase documentation on User Based Security 中的一些示例。

      【讨论】:

      • 不工作。我找到了解决方案,改为使用 Firebase 我使用 DatabaseReference。
      猜你喜欢
      • 2020-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-10-03
      • 1970-01-01
      • 1970-01-01
      • 2015-02-12
      • 2019-04-18
      • 2019-04-09
      相关资源
      最近更新 更多