How to read collection only if UID matches collection name in Firebase Firestore?

和自甴很熟 提交于 2021-01-29 06:33:48

问题


I created a Collection of user IDs for each user and I want to set the database rules to read the collection item only if the user ID matches the collection item name. I tried something like:

 service cloud.firestore {
    match /databases/{database}/documents {
      match /{document=**} {
        allow read: if request.auth != null;
      }
      match /{userId} {
        allow read: if belongsTo(userId);
      }
      function belongsTo(userId) {
        return request.auth.uid == userId
      }
    }
    }

回答1:


Firestore reads documents. There is no concept of reading a collection. So your rules must provide access to documents.

To grant access to all documents in the collection named after the user's UID:

  match /{userId}/{document} {
    allow read: if belongsTo(userId);
  }


来源:https://stackoverflow.com/questions/55991827/how-to-read-collection-only-if-uid-matches-collection-name-in-firebase-firestore

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