Firebase chat app setValue failed error with a pub

2019-01-02 16:04发布

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

2条回答
公子世无双
2楼-- · 2019-01-02 16:42

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

查看更多
泛滥B
3楼-- · 2019-01-02 16:43

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:

Cloud Firestore rules in Firebase console

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.

Switching to Realtime Database rules in Firebase console

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.

查看更多
登录 后发表回答