I'm creating an android app using firebase. My database structure is like this,
user:{
$uid:{
uname: abc,
age: 20,
email: [email protected],
premium: true,
score: 80
}
}
And security rules look like this,
{
"rules": {
"user":{
"$uid":{
"score":{
".read":"auth.uid===$uid",
".write":"auth.uid===$uid"
},
"$others":{
".read":"auth!=null",
".write":"auth!=null"
}
}
}
}
}
And this is the code I read data from firebase,
DatabaseReference myRef = FirebaseDatabase.getInstance().getReference()
.child("user").child(uid);
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
users = dataSnapshot.getValue(User.class);
//set values to associated textviews
}
@Override
public void onCancelled(DatabaseError databaseError) {}
});
The problem is ".read" rule only in simulator, although ".write" rule work in both app and simulator. When I change the rule to this,
{
"rules": {
"user":{
"$uid":{
".read":"auth!=null",
".write":"auth!=null",
"score":{
".read":"auth.uid===$uid",
".write":"auth.uid===$uid"
},
"$others":{
".read":"auth!=null",
".write":"auth!=null"
}
}
}
}
}
Its work for both read and write.But this is not secure. Why can't I read data even auth.uid = myUid? However ".write" rule always goes fine.