9
votes

I've been using FireStore for a project that works fine in the browser, but when I port the code to Expo, running on a iOS 11.2 iPhone X in the simulator, it keeps raising Error: Missing or insufficient permissions.

Auth is working fine, and when I check the client in the Firestore collection object, the appropriate UID is set, and testing the same code in the browser, everything works perfectly (no permissions problem). What I'm trying to say is that I'm 95% certain that the issue is with the Firebase lib/react native/expo combination, rather than with my code. Especially since it seems that making a call to Firestore in the browser, there are headers set, but when debugging the call in Reactotron (from Expo), it looks like the call the Firebase lib is making has no headers at all.

I doubt it makes a difference, but here are my auth rules:

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

The call I was trying to make (and there is definitely data in the doc) is the following:

profile = (await UserDataCollection.doc(`${idToken.uid}`).get()).data();

I'm curious if anyone else has run into this problem, and if so, are there any workarounds to make Firestore work?

1
I am having exactly the same problem you are facing. It's been happening ever since I upgraded react-native (from 0.35 to 0.51 so a lot of changes, I know). I've already posted about it in this Stack Overflow post. I know it's not my security rules because an older build is working fine. I'm just using the Firebase web api with React native. I haven't been able to identify the problem so far but I was assuming it was an issue with an incorrect/expired auth header. - JamesR
@JamesR glad to hear I'm not the only one. I was kicking myself trying to figure out what was different. I'm not sure how best to analyse requests in React Native, but I've given Reactotron a shot and it seems the Firestore requests don't have any headers set at all. I'm curious if that's what your Firestore requests look like too? - raphaeltm
Well as per my linked post I used Charles to intercept and inspect my requests, from what I could see it looked like my headers were being set (but no easy way to tell if they're correct as far as Firestore is concerned). Charles isn't the prettiest UI to inspect though so I may have missed something - JamesR
I'm a little delirious from lack of sleep but I managed to get it fixed by downgrading from 4.8.1 to 4.6.2. I was really curious as to why this was happening though and the only weird thing I was noticing in Charles was that the latest version was always sending the bearer token and it seemed to have a weird auth header "X-Goog-Api-Client:gl-js/ fire/4.8.1" (note the space). The downgraded version didn't seem to send the auth header but just the sessionId. That's all I have right now, I'll check it tomorrow when I'm better rested. What version of Firebase are you using? - JamesR
@JamesR Awesome! Downgrading to 4.6.2 seems to have done the trick for me too. I don't know how much time I'll have to dig through and see what specific change made that happen, but I'll see if I can take a look in the coming weeks. If you find out, it would be great to hear what you discover! - raphaeltm

1 Answers

8
votes

As per previous comments, downgrading to 4.6.2 seems to fix the issue. I'm not sure the exact root cause but the behaviour of how the auth headers are sent seem to have changed (so potentially something there?). I'll update my answer if I get time to investigate further. Happy for one of the Firebase team to contact me for my account details if they want to verify on their side.