0
votes

enter image description here

I have a really simple firestore db that looks like the image above.

I want to write a security rule so that only authenticated users can get in, but whatever I write, I always get permission denied.

I have tried:

rules_version = '2';
service cloud.firestore {
  match /users/{user} {
   allow read, write: if request.auth.uid == user
   match / {docs = **} {
      allow read, write: if request.auth.uid == user
   }
 }
}

I also tried:

rules_version = '2';
service cloud.firestore {
 match /users/{user} {
   allow read, write: if request.auth.uid == user
   match / {docs = **} {
      allow read, write: if request.auth.uid == user
   }
 }
}

I also tried:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /users/{userId}/{documents=**} {
      allow read, write: if isOwner(userId);
    }
  }

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

This does not work:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /sessions/{sessionID} {
      allow read, write: if request.auth != null;
    }
  }
}

Neither does this:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /sessions/{sessionsID} {
      allow read, write: if request.auth != null;
    }
  }
}
1
I can't see the word sessions throughout the post. Or maybe you posted any other screenshot? - Dharmaraj
I haven't used the word sessions in the rules at all - but I thought I was selecting all documents however they were nested in folders? - Davtho1983
Not sure, all my rules in firestore start with DB name - Dharmaraj
Can you give me an example that works for you? All I need is to allow read, write for authenticated users - Davtho1983
Are those User UID in side sessions doc? - Dharmaraj

1 Answers

1
votes

I tried to compare my rules with your database structure.

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /sessions/{sessionID=**} {
      allow read, write: if request.auth.uid != null;
    }
}

Now this should allow only registered users to gain access.