Davtho1983
Davtho1983

Reputation: 3954

Firebase firestore security rules not working

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;
    }
  }
}

Upvotes: 0

Views: 674

Answers (1)

Dharmaraj
Dharmaraj

Reputation: 50930

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.

Upvotes: 1

Related Questions