Morad Abdelgaber
Morad Abdelgaber

Reputation: 21

firestore : Missing or insufficient permissions

I'm using Role ( allow read ,write : if request.auth.uid != null ) when I logged in I get data it's ok , but when I log out the user I get the error : Missing or insufficient permissions. first I thought it was because of I didn't unsubscribe the Observable I tried (rxjs/operator/takeWhile) even if I used async pipe, I got the same error.

Upvotes: 2

Views: 521

Answers (1)

Travis Beck
Travis Beck

Reputation: 1128

if you're logged out then request.auth.id == null, therefore, your read rule returns false. try changing to:

allow read;
allow create: if request.auth.uid != null; 

This allows anyone to read and only authenticated users to create. You'll usually only want authors to update. For that you'll need to save the author's uid on the document as a property like userid, then you can update your rules like this:

match /myCollection/{document=**} {
  allow read;
  allow create: if request.auth.uid != null; 
  allow update: if isOwner();
}

function existingData() {
  return resource.data
}

function isOwner() {
  return request.auth.uid == existingData().userid;
}

Upvotes: 2

Related Questions