Skip to content
Advertisement

I can`t read the data after I update my rules on firestore (React Native)

the rules I set is allow read: if resource.data.uid == request.auth.uid;

how I call it is like this

return async (dispatch) => {
    await firebase
      .firestore()
      .collection("patients")
      .doc(firebase.auth().currentUser.uid)
      .collection("patient")
      .orderBy("creation", "desc")
      .get()
      .then((result) => {
        let Patients = result.docs.map((doc) => {
          const data = doc.data();
          const id = doc.id;
          return { id, ...data };
        });
        dispatch({ type: GET_PATIENTS, Patients });
      });
  };

How can i fix it?

firebase rule

 match /databases/{database}/documents {
    match /patients/{patientsID}{
        match /patient/{patientID}{
        allow read: if resource.data.uid == request.auth.uid;
        allow create: if request.resource.data.uid == request.auth.uid;
        allow update: if request.resource.data.uid == request.auth.uid;
        allow delete: if resource.data.uid == request.auth.uid;
        match /diagnostic/{diagnosticID}{
          allow read: if true;
          allow write: if request.resource.data.uid == request.auth.uid;
        }
      }
    }
  }
}

image for the data

https://1drv.ms/u/s!Ag7uBMx2FuEijuILje2xJIv8RawcFA?e=Gy6jeC

Advertisement

Answer

Rules don’t filter data on their own. Instead they merely ensure that the data that is being requested matches your rules.

So your query needs to match the rules, which means they also need to filter by UID:

  .collection("patient")
  .orderBy("creation", "desc")
  .where("uid", "==", firebase.auth().currentUser.uid)
Advertisement