Harsha Aithal
Harsha Aithal

Reputation: 103

PERMISSION_DENIED: Missing or insufficient permissions

I am running the following firebase code

    FirebaseApp app = await FirebaseApp.configure(
          name: 'weather app',
          options: const FirebaseOptions(
            googleAppID: '1:nnnnnnnnn:android:nnnnnnn',
            apiKey: 'XXXXXXXX',
            projectID: 'XXXXX-XXX-XXX08',
          ),
        );
        Firestore firestore = Firestore(app: app);
        FirebaseAuth.fromApp(app).signInAnonymously();

if I run this code for the first time in the emulator, it is throwing the following error

 I/flutter (17766): getConfigState ERROR 2 PlatformException(Error performing get, PERMISSION_DENIED: Missing or insufficient permissions., null)
W/Firestore(17766): (21.3.0) [Firestore]: Listen for Query(weather-api-info/owm) failed: Status{code=PERMISSION_DENIED, description=Missing or insufficient permissions., cause=null}

But if I simply reload the app using hot reload or full reload then it doesn't complain. Again, the issue is occuring only when the app runs for the first time on the device (i can re-produce this by uninstalling the app and trying again)

Cloud firestore auth look like below(I am not planning to set up any sign-in or auth, but don't want to keep it open as well

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

Upvotes: 0

Views: 2454

Answers (1)

Harsha Aithal
Harsha Aithal

Reputation: 103

So, I was able to fix this myself. In a nutshell, I did not set up the code properly for async calls. Following is what I modified (compare the code mentioned below to the one in the original post)

Future<void> getOWMFirestoreData() async {
  try {
    FirebaseApp app = await FirebaseApp.configure(
      name: 'weather app',
      options: const FirebaseOptions(
            googleAppID: '1:nnnnnnnnn:android:nnnnnnn',
            apiKey: 'XXXXXXXX',
            projectID: 'XXXXX-XXX-XXX08',
      ),
    );
    await FirebaseAuth.fromApp(app).signInAnonymously();
    Firestore firestore = Firestore(app: app);

    var docs =
        await firestore.collection('XXX-info').document('owm').get();
    debugPrint('getOWMFirestoreData Succesfull ${docs.exists}');

Upvotes: 3

Related Questions