Firebase chat app setValue failed error with a public database?

删除回忆录丶 提交于 2019-11-26 01:38:14

问题


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);
}

回答1:


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.




回答2:


change this

request.auth.uid != null

to

request.auth.uid == null

or defined a proper auth mechanism before starting the conversation where user defined by userID



来源:https://stackoverflow.com/questions/52126720/firebase-chat-app-setvalue-failed-error-with-a-public-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!