0
votes

Not sure why I'm getting this error while accessing AngularFirestoreCollection:

ERROR Error: Missing or insufficient permissions.

this.categories = afs.collection('categories').doc(this.afAuth.auth.currentUser.uid).collection<Category>('categories');

Firestore Database Rules:

service cloud.firestore {
  match /databases/{database}/documents {
    match /categories/{userId}{
      allow read, write: if request.auth.uid == userId;
    }
  }
}

What am I missing here?

2
From your code it looks like you're trying to access /categories/{userId}/categories, yet your rule is only for /categories/{userId}. Try changing it to match /categories/{category=**} {. - Joseph Webber

2 Answers

1
votes

You have configured a deep path in your application:

Categories --> User --> Categories

And your rules only contains the parent folder which is:

/categories/{userId}

You have to change to:

/categories/{userId}/categories/{category}

So you have access to UserId for the matching condition, plus to the effect of the current path.

There are many reasons when to use wildcards and when to use specific or nested matching conditions, Take a look at the main docs and choose your best fit implementation.

0
votes

Had the same issue.

Check before you making the call if you actually already signed in :

isSignedIn() {
  this.auth.onAuthStateChanged((user) => {
    if (user) {
    // User is signed in.
    console.log("User is signed in.");

    } else {
      console.log("No user is signed in.");
    }
});