I want my users in my flutter app to be able to access other users recipes if the other user allowed them. I am using Firebase Cloud Firestore as the DB. So if the current user is part of the requested users usersAllowedToRead collection then they can also access the other users recipes.
match /databases/{database}/documents {
match /users/{userId} {
// Allow the user to read and write their own document
allow read, write: if request.auth != null && request.auth.uid == userId;
match /recipes/{recipeId} {
// Allow read access if the user is authenticated and either is the user document owner
// or is listed in the 'usersAllowedToRead' subcollection of the user document
allow read: if request.auth != null && (request.auth.uid == userId || exists(/databases/$(database)/documents/users/$(userId)/usersAllowedToRead/$(request.auth.uid)));
// Allow write access only if the user is authenticated and is the user document owner
allow write: if request.auth != null && request.auth.uid == userId;
}
}
}
But I always get an error stating that access is denied. When I tested it in the firebase rule playground then the exists evaluates to false even when the current user is present in the other user’s usersAllowedToRead collection.
This path exists in my db: /users/VJ1DcW.../usersAllowedToRead/MmrFH...
but when I execute a get call then the exists method evaluates to false and I can’t figure out why.