Search code examples
javascriptangulargoogle-cloud-firestorefirebase-securityangularfire

Missing or insufficient permissions with AngularFirestore


I'm getting following error when I try to get doc form Firestore (angular@12.0.3 and angular/fire@6.1.5):

ERROR FirebaseError: Missing or insufficient permissions.

According to firebase's manual, I wrote rules for firestore:

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

Here is the code from my service:

constructor(
    private store: AngularFirestore,
    private db: AngularFireDatabase,
    private auth: AuthService
  ) {
    // this.auth.user$ = this.angularFireAuth.user
    this.auth.user$.subscribe(user => {
      if (user && user.email) {
        this.store.doc('/collection/document').valueChanges().subscribe(_ => {
          console.log(_); // throws ERROR FirebaseError: Missing or insufficient permissions.
        });

        this.db.list<List>(`lists`, ref => ref.orderByChild('owner').equalTo(user.email))
          .valueChanges([], {idField: 'uuid'})
          .subscribe(_ => {
            console.log(_); // works fine
          })
      }
    });
}

I'm a bit familiar with firebase rules, so subscription on rtdb works fine (with production-ready rules). And I can receive data from firestore collection if I change read rules with allow read: if true;, but of course I want rules to be more strict. So the problem looks like request.auth in rules seems to equal null without adequate reason.

Thanks everyone who can help!


Solution

  • The problem is in the firebase package : https://github.com/angular/angularfire/issues/2838

    I did have the same problems after updating all my packages (angular 12, angular/fire, firebase) It was resolved when i downgraded the fireabase package:

    npm i firebase@8.6.1