Skip to content

Angularfire2 shows “Missing permissions” error, whilst straight firebase runs with no issue #2121

@adzza24

Description

@adzza24

Version info

Angular: 6.0.3

**Firebase:**6.2.0

**AngularFire:**5.2.1

How to reproduce these conditions

To recreate the issue

In my AuthService

constructor(
  private afAuth: AdminAngularFireAuth,
  private afStore: AngularFirestore
) {
  this.user$ = this.afAuth.authState.pipe(switchMap((user: firebase.User) => {
    if (!user) {
      return of(null);
    }
    return this.afStore.collection("users").doc(user.authId).valueChanges()
  }));
}

This was working fine until I added the rules to firebase (see below). Now when requesting the user doc I get a Missing or insufficient permissions error.

I know what you'd think - my rules are inicorrect. But I have tested the rules in firebase using the simulator AND I have tested them using pure firebase from my front end app like this:

var email = "xxx@xxx.com";
var password = "xxx";
var firebaseConfig = {...};

firebase.initializeApp(firebaseConfig);

firebase.auth().onAuthStateChanged((u) => {
  if (u) {
    firebase.firestore().doc('users/' + u.uid).get()
      .then(u => console.log("Got favorites", u.data()));
    firebase.firestore().doc('favorites/' + u.uid).get()
      .then(u => console.log("Got favorites", u.data()));
  }
});

firebase.auth().signInWithEmailAndPassword(email, password).then(cred => {
  console.log("Signed in", cred)
})

This works. So it is not a firebase permissions issue, it is something that AngularFire is doing which pure firebase does not. If it makes any difference it seems to happen more after I have logged out with one user and then try to log back in with another, but no amount of cache/cookie clearing seems to help. Not even using a different browser. Just randomly a day later it might start working again, but then when I log out and back in again the same thing happens.

What sometimes happens is I log out of User1, login to User2. Then I logout of User2 and try to log back in to User1. Now the same user I was just logged into gets permissions error's access their documents. The authorisation is successful, but the subsequent request to a firestore document is denied. When only moments ago the exact same request was allowed. This has to be a bug... It feels like somewhere a reference is being stored to the session and not wiped. So even though the user has changes, maybe Anguarfire is sending old info in the request and thus it gets denied. I don't know how to test this or diagnose it though.

Sample data and security rules

Data structure:

{
    users: <Collection>{
        userId1: <Document>{
            firstName: "John",
            lastName: "Doe",
            authId: "userId1"
        }
    }
}

Security Rules:

service cloud.firestore {
    match /databases/{database}/documents {

        // Re-usable helper function to retrieve current user record
        function getUser() {
            return get(/databases/$(database)/documents/users/$(request.auth.uid))
        }

        match /users/{userID} {

            // Users can view/edit themselves
            allow read, write: if getUser().data.authId == userID;
      
            // Admins can view/edit anyone
            allow read, write: if getUser().data.role == 0;
        }
    }
}

Expected behavior

Documents should be accessible from the front end app via AngularFire2

Actual behavior

The request is denied with a Missing or insufficient permissions error, while pure firebase works...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions