rvr93
rvr93

Reputation: 913

code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null

I am not able to read/write data to firestore with rules in place.

I can access my data and do operations on it with no rules set as shown below:

match /{document=**} {
  allow read, write: if true;
}

But I am getting the "permission denied" error with following rules.

match /{document=**} {
  allow read, write: if request.auth.uid != null;
}

Here is my dart code:

if (uid == null) {
  user = await a.currentUser();
  uid = user.uid;
}
DocumentReference docRef = firestore.collection('users').document(uid);
CollectionReference locations = docRef.collection('locations');


await locations.document().setData(<String, double>{
  'latitude': latitude,
  'longitude': longitude,
});

I can think of two scenarios in which this request fails:

  1. request.auth.uid == null. But I have a user logged in with firebase auth before this query.
  2. My security rules and query doesn't match.

Please help me debug this issue.

Edit 1:

  1. I have user authenticated during my firebase query as I changed the code to enter only if the user is authenticated as below:
    User u = await a.onAuthStateChanged.first;

    if (u != null) {....}
  1. I was able to read/write with authentication using firebase simulator.

Is there anyway I can print the actual request being sent to the firebase from dart using cloud_firestore package?

Edit 2

  1. Firestore.instance is working with rules
  2. Firebaseapp configured to my specific app using configure,options isn't working.

Any help on why this might be happening?

Upvotes: 1

Views: 6162

Answers (2)

Jay Mungara
Jay Mungara

Reputation: 7150

Just change your rules to as below if you want to allow anybody to upload the documents.

allow read, write;

If you want to allow only authenticated users to allow upload the files or documents to your firebase change your rules to

allow read, write: if request.auth != null;

Upvotes: 3

griffins
griffins

Reputation: 8274

change your rules to allow read,write: if request.auth !=null;

Upvotes: 1

Related Questions