Firestore нет разрешений на выполнение операции - PullRequest
0 голосов
/ 01 апреля 2020

Я пытаюсь настроить правила в Firestore, где каждый может читать содержимое друг друга, если они аутентифицированы в приложении, но только владельцы документов могут создавать, писать, обновлять или удалять их.

Я установил следующие правила в Firestore:

rules_version = '2';

service cloud.firestore {
  match /databases/{database} {
    match /codes/{userID} {
      allow create, write, update, delete: if request.auth.uid == userID;
      allow read: if request.auth.uid != null;
    }
  }
}

Моя структура Firestore:

Collection 'codes' > Document 'BNhBibYZ0ThCNCH2gzPRufFsIk22' > nothing in here

Мой запрос:

firestore()
  .collection('codes')
  .doc(this.state.userID)
  .onSnapshot((querySnapshot) => {
    if (!querySnapshot) {
      let testObj = {"hello": "world"} 
      firestore().collection('codes').doc(this.state.userID).set(testObj);
    }
  });

Вывод, который я получаю: Error: [firestore/permission-denied] The caller does not have permission to execute the specified operation.

Что я делаю не так?

1 Ответ

1 голос
/ 01 апреля 2020

Вы можете использовать следующие функции, которые я создал, чтобы сделать это

function isUserAuthenticated() {
    return request.auth.uid != null; 
}

function belongsTo(userId) {
    return request.auth.uid == userId;
}

Затем вы можете использовать его так:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if false;
    }

    match /codes/{userId} {
      allow read : if isUserAuthenticated();
      allow create : if belongsTo(userId);
      allow update: if belongsTo(userId);
      allow delete: if belongsTo(userId);
    }

    /* Functions */
    function isUserAuthenticated() {
      return request.auth.uid != null; 
    }

    function belongsTo(userId) {
      return request.auth.uid == userId;
    }
  }
}   

Принять как ответ, если он работает для вы. Доступно для большего количества вопросов в комментариях.

...