Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set Firestore security rules? resource.data: Null value error

I need some help making my security rules for firestore work.

These are my firestore rules:

service cloud.firestore {
  match /databases/{database}/documents {
     match /orders/{orderID} {
       allow read, update: if  request.auth.uid == resource.data.buyerId  || request.auth.uid == resource.data.sellerId;
    }
  }
}

my orders collection:

orders: {
sellerId: 'some-id',
createdAt: timestamp,
buyerId: 'some-id'
}

It should return all documents from orders collection which has either buyerId or sellerId equal to authorised user (request.auth.uid).

but the above rule is not working as expected.

firestore collections screenshot

firebase simulator output

like image 311
Mohd Imran Avatar asked May 02 '19 11:05

Mohd Imran


People also ask

What are the FireStore security rules?

Cloud Firestore security rules evaluate each query against its potential result and fails the request if it could return a document that the client does not have permission to read. Queries must follow the constraints set by your security rules. For more on security rules and queries, see securely querying data.

How are access control information stored in Cloud Firestore?

Many apps store access control information as fields on documents in the database. Cloud Firestore Security Rules can dynamically allow or deny access based on document data: The resource variable refers to the requested document, and resource.data is a map of all of the fields and values stored in the document.

What is the request resource object in FireStore?

The request.resourceobject is the document that is being sent in the request to your database. rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /petowners/{ownerId} { request.auth.uid == request.resource.data.userId; } } } Using Functions

How do you verify a value type in FireStore?

Verifying a Value’s Type One of the great things about Firestore rules is that you can deny the creation or modification of a document, if the value being provided in the request isn’t what you expect it to be. One way to check for this, is to check for the type of the value.


Video Answer


2 Answers

resource.data: Null - this error happens when you try to create a new entity.

Split write rule, on create and update.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /user/{userId} {
      allow read: if request.auth.uid == userId;

      function authed() {
        return request.auth.uid == userId;
      }
      allow create: if authed() && request.resource.data.keys().hasOnly(['name']);
      allow update: if authed() && request.resource.data.diff(resource.data).changedKeys().hasOnly(['name']);
      allow delete: if authed();
    }
  }
}
like image 64
Oleksii.B Avatar answered Oct 12 '22 08:10

Oleksii.B


That error message is suggesting that the requested document was not actually present in the database. You entered "orders/{orderId}", which looks like you put a wildcard in the Location field in the simulator. That's not going to work. You need to enter the path to an actual document that exists if you want to test your rule that uses its field values.

like image 12
Doug Stevenson Avatar answered Oct 12 '22 10:10

Doug Stevenson