0
votes

I am analysing the get started guide for firebase cloud firestore. This is for an android app. https://firebase.google.com/docs/firestore/security/get-started

I need to save and read the user data from every android device for backing up some data. The document for firebase cloud firestore shows the below code for accessing the data for all users. But I want to restrict the user data to be private so that it can be accessed only by the user who saved it. Please advice how to proceed with the access / authentication process.

// Allow read/write access on all documents to any user signed in to the application
service cloud.firestore {
    match /databases/{database}/documents {
        match /{document=**} {
            allow read, write: if request.auth.uid != null;
        }
    }
}
1

1 Answers

2
votes

If you store in the document the uid of the user who saved the doc in a field called (for example) createdBy you can do as follows:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read: if request.auth.uid != null && resource.data.createdBy == request.auth.uid;
      allow write: .... 
    }
  }
}