公共数据库的 Firebase 聊天应用 setValue 失败错误? [英] Firebase chat app setValue failed error with a public database?

查看:16
本文介绍了公共数据库的 Firebase 聊天应用 setValue 失败错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 Firebase 的聊天应用,它一直在使用

<块引用>

setValue at x 失败:数据库错误:权限被拒绝

每次输入消息都会出错.

我已经将我的数据库设置为公开:

service cloud.firestore {匹配/databases/{database}/documents {匹配/{allPaths=**} {允许读、写:如果 request.auth.uid != null;}}}

是不是来自我的聊天参考资料?

private void displayChat() {ListView listOfMessage = findViewById(R.id.list_of_message);查询查询 = FirebaseDatabase.getInstance().getReference();FirebaseListOptions<聊天>options = new FirebaseListOptions.Builder().setLayout(R.layout.list_item).setQuery(查询,Chat.class).建造();适配器 = 新 FirebaseListAdapter(选项){@覆盖protected void populateView(View v, Chat model, int position) {//获取list_item.xml视图的引用TextView messageText, messageUser, messageTime;messageText = v.findViewById(R.id.message_text);messageUser = v.findViewById(R.id.message_user);messageTime = v.findViewById(R.id.message_time);messageText.setText(model.getMessageText());messageUser.setText(model.getMessageUser());messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));}};listOfMessage.setAdapter(适配器);}

解决方案

您的代码正在使用 Firebase 实时数据库,但您正在更改 Cloud Firestore 的安全规则.虽然这两个数据库都是 Firebase 的一部分,但它们完全不同,其中一个的服务器端安全规则不适用于另一个.

当您进入 Firebase 控制台中的数据库面板时,您很可能最终会进入

如果您使用

您也可以通过点击此链接.

与您所拥有的相匹配的实时数据库的安全规则是:

<代码>{规则":{".read": "auth.uid !== null",".write": "auth.uid !== null"}}

这将授予任何经过身份验证的用户对整个数据库的完全读写访问权限.阅读我对此问题的回答,了解有关此类规则的安全/风险权衡的更多信息:Firebase 电子邮件说我的实时数据库有不安全的规则.

I have a chat app using Firebase that keeps on having a

setValue at x failed: DatabaseError: permission denied

error every time I type a message.

I set my Database to be public already:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{allPaths=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

Is it something from within my chat reference?

private void displayChat() {

    ListView listOfMessage = findViewById(R.id.list_of_message);

    Query query = FirebaseDatabase.getInstance().getReference();
    FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
            .setLayout(R.layout.list_item)
            .setQuery(query, Chat.class)
            .build();

    adapter = new FirebaseListAdapter<Chat>(options) {
        @Override
        protected void populateView(View v, Chat model, int position) {
            //Get reference to the views of list_item.xml
            TextView messageText, messageUser, messageTime;
            messageText = v.findViewById(R.id.message_text);
            messageUser = v.findViewById(R.id.message_user);
            messageTime = v.findViewById(R.id.message_time);

            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", model.getMessageTime()));
        }
    };
    listOfMessage.setAdapter(adapter);
}

解决方案

Your code is using the Firebase Realtime Database, but you're changing the security rules for Cloud Firestore. While both databases are part of Firebase, they are completely different and the server-side security rules for one, don't apply to the other.

When you go the database panel in the Firebase console, you most likely end up in the Cloud Firestore rules:

If you are on the Cloud Firestore rules in the Firebase console, you can change to the Realtime Database rules by clicking Cloud Firestore BETA at the top, and then selecting Realtime Database from the list.

You can also directly go to the security rules for the Realtime Database, by clicking this link.

The security rules for the realtime database that match what you have are:

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

This will grant any authenticated user full read and write access to the entire database. Read my answer to this question on more on the security/risk trade-off for such rules: Firebase email saying my realtime database has insecure rules.

这篇关于公共数据库的 Firebase 聊天应用 setValue 失败错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆