What rule should I use if I want only authentication user can write the data into firebase and then the authentication user can read the data from other authentication user. For example User A write a data into database, I want User B also can read data from User A.
For example JSON:
{
"User Information" : {
"47NiYA7rGNa8nJvlnnxzpxhqvCl2" : {
"-LYuXT8d5Hzm4Sp_7buK" : {
"deceasedName" : "You",
"deceasedRelative" : "Are",
"noLot" : "34",
"relativeAddress" : "Main Road",
"relativeContact" : "0231456789"
}
},
"LV4y2BXnNWWkJix6YwTRJV8LdQs2" : {
"-LYufF_KMO1o7EULIK8Y" : {
"deceasedName" : "amir",
"deceasedRelative" : "loqman",
"noLot" : "104",
"relativeAddress" : "aman",
"relativeContact" : "123456"
}
}
}
}
For table "User Information" there are 2 authentication user which is Uid = "47NiYA7rGNa8nJvlnnxzpxhqvCl2" and Uid = "LV4y2BXnNWWkJix6YwTRJV8LdQs2". So I want each user can retrieve every data in "User Information" table.
This code only allowed user to retrieve their own data:
databaseReference = FirebaseDatabase.getInstance().getReference("User Information");
FirebaseUser user = firebaseAuth.getCurrentUser();
String UserID = user.getUid();
databaseReference.child(UserID).addListenerForSingleValueEvent(new ValueEventListener() { {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot INFOSnapshot : dataSnapshot.getChildren()){
UserInformation userInfo = INFOSnapshot.getValue(UserInformation.class);
infoList.add(userInfo);
}
UserInfoAdapter userInfoAdapter = new UserInfoAdapter(InfoDisplayActivity.this, infoList);
listViewINFO.setAdapter(userInfoAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
I use this firebase rule:
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
So do I need to change the firebase rule or change the coding?