Issue in authentication in firebase cloud firestore at user level

冷暖自知 提交于 2020-01-06 12:42:35

问题


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:


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: .... 
    }
  }
}


来源:https://stackoverflow.com/questions/54114230/issue-in-authentication-in-firebase-cloud-firestore-at-user-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!