I have a users
collection which has documents containing various fields. Each document ID is the firebase user ID and the fields hold user data.
I am using the following rule:
match /databases/{database}/documents {
// Make sure the uid of the requesting user matches name of the user document.
match /users/{userId} {
allow read, update, delete: if request.auth.uid == userId;
allow create: if request.auth.uid != null;
}
}
Users can select "favourite songs" which are saved within an array field. I now have a requirement to query the database for users which have added a certain songs to their favorites. I created the following query:
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference usersRef = db.collection("users");
usersRef.whereArrayContains("favouriteSongs", "id_of_song");
When the query is run the following error is returned:
com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions.
I obviously need to alter my Firestore rule to allow a user to query the "favouriteSongs" field; however, I do not want to expose all user data to everyone.
Is there a way to expose the "favouriteSongs" data (which only holds IDs) and ensure the rest of the user collection is restricted from being read?
Thanks