I have this TypeScript React code, for fetching a firestore collection (using react-firebase-hooks):
const [membersSnapshot, loading, error] = useCollectionData(
query(
collection(db, USERS_COLLECTION).withConverter(UserConverter.prototype),
where(UserFields.FamilyId, "==", familyId)
)
);
console.assert(error === undefined, error);
Which without rules, works as expected (assertion not failing).
I have added this rule (I’m adding only the relevant parts):
function getUser() {
return getUserData(request.auth.uid);
}
function getUserData(userId) {
return get(/databases/$(database)/documents/users/$(userId)).data;
}
function isSignedIn() {
return request.auth != null;
}
function inSameFamily(userId) {
return getUser().familyId == getUserData(userId).familyId;
}
match /users/{userId} {
allow read: if isSignedIn() && request.auth.uid == userId || isSignedIn() && inSameFamily(userId);
}
With this rule, the assertion fails,
Assertion failed: FirebaseError: Missing or insufficient permissions.
and I do not understand what’s the problem.
I tried to change the where
condition, to:
where(UserFields.Email, "==", userEmail)
and expected it to not fail when I’m trying to get the user itself, but it failed.
Please help…