Reputation: 1729
My firebase database is something like this:-
these are the security rules i have written but when i use them in simulator they validate just fine, but when used in code, give permission denied error
{
"rules": {
"jokes" : {
"textjokes" : {
".read" : true,
".write": "auth != null"
}
},
"users": {
"$uid": {
".write": "$uid == auth.uid"
}
},
"userslikelist": {
"$uid": {
".write": "$uid == auth.uid"
}
}
}
}
I get error:-
/userslikelist/qzCqJph6XcZbiIpbDKTTxwjRHrh1/-KK850oXwPnOvxan1xNG failed: FirebaseError: Permission denied
/jokes/textjokes/MISC-0-01/likeCount failed: FirebaseError: Permission denied
My code:-
Firebase ref = new Firebase("https://xxxxxxxxx.firebaseio.com/userslikelist/" + mAuth.getCurrentUser().getUid());
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
if(!likedMap.containsKey(postSnapshot.getKey())){
likedMap.put(postSnapshot.getKey(), postSnapshot.getValue(String.class));
}
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
I dont get it. what am i doing wrong?
Updated Code:-
{
"rules": {
"jokes": {
"textjokes": {
".read": true,
".write": "auth != null"
}
},
"users": {
"$uid": {
".write": "$uid == auth.uid"
}
},
"userslikelist": {
"$uid": {
".read": "$uid == auth.uid",
".write": "$uid == auth.uid"
}
}
}
}
Upvotes: 0
Views: 1137
Reputation: 1729
For anyone else who is having this problem. The issue here was that i was using the legacy firebase api methods to set values and read from database. I changed the method of updating to use the new google firebase system as used in this example and now i can update the database.
Upvotes: 1
Reputation: 9389
You are not giving read access to /userslikelist/userId
so you wont be able to get dataSnapshot
on your onDataChange
.
Make sure to set a read permission on the /userslikelist/userId
level.
{
"rules": {
"jokes" : {
"textjokes" : {
".read" : true,
".write": "auth != null"
}
},
"users": {
"$uid": {
".write": "$uid == auth.uid"
}
},
"userslikelist": {
"$uid": {
".read": "$uid == auth.uid", //do whatever logic you need here
".write": "$uid == auth.uid"
}
}
}
}
Upvotes: 1