Home > Enterprise >  FirebaseException ([cloud_firestore/permission-denied] The caller does not have permission to execut
FirebaseException ([cloud_firestore/permission-denied] The caller does not have permission to execut

Time:01-20

I get this exception using Firebase Firestore. I have tried everything and every possible rule for Firestore but still does not work. What I'm trying to do is, getting a user from a collection and then checking the value of it with Riverpod state manager.

Getting a user from Firestore:

      Future<UserModel?> getUserData() async {
      DocumentSnapshot<Map<String, dynamic>> userData =
          await firestore.collection('users').doc(auth.currentUser?.uid).get();
      UserModel? user;
      if (userData.data() != null) {
        user = UserModel.fromMap(userData.data() as Map<String, dynamic>);
      }
      return user;
     }

Using a provider to get the value and then returning a screen:

      home: ref.watch(userDataAuthProvider).when(
            data: (user) {
              if (user == null) {
                    return const RegisterScreen();
                  } else {
                    return const HomeScreen();
                  }
                },
                error: (err, trace) {
                  return Center(child: Text(err.toString()));
                },
                loading: () => const CircularProgressIndicator(),
              ),

Firestore rules:

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

CodePudding user response:

Open your firebase console and Go to Firebase Datastore and make changes below in Rules.

Edit Rules and change like this

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

I Hope this things are solve your issue.

https://i.stack.imgur.com/fkivo.png

  • Related