1
votes

I have a simple database structure that i want to add firebase security rules to but the rules is blocking all the permission even when i set the value of .read and .write to true under the node structure, Here is the sample of the rule

{
  "rules": {
    "Lines": {
      ".read": true,
        ".write": false
    },
      "Links": {
      ".read": true,
        ".write": false
    }
  }
}

Here is a Sample of My Database Structure

{
  "Lines" : {
    "Line 1" : "Lines 1238443",
    "Line 2" : "Lines 4657673"
  },
  "Links" : {
    "Links 1" : "Link 3282873",
    "Links 2" : "Link 3493934"
  }
}

I am trying to allow only read operations but my application keeps saying permission is denied i have check the documentation it looks straight forward but i can't tell what exactly is happening because even when i tried

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

I still get permission denied error Here is the code to read from the database

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
        databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                String lin = snapshot.child("Links").child("Links 1").getValue(String.class);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                
                Toast.makeText(getApplicationContext(), "Error "+error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
1
Can you show the code used to read the data? - Renaud Tarnec
I have updated the question please do recheck - Serenity Emmanuel

1 Answers

2
votes

You're trying to read from the root of the database. And since your rules don't grant anyone read access to the root of the database, the read is rejected.

If you only want to read the Links child from the database, you should specify that child name before attaching a listener, so:

DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();

databaseReference.child("Links").addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        String lin = snapshot.child("Links 1").getValue(String.class);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {        
        Toast.makeText(getApplicationContext(), "Error "+error.getMessage(), Toast.LENGTH_SHORT).show();
    }
})

Now we're attaching a listener to /Links, where the security rules do allow reading the data.