0

My Flutter App works perfectly when it is connected to my first Cloud Firestore Database (which is App Check enforced).

I managed to connect my App to a second Cloud Firestore Database. However, after enforcing App Check for the second database, despite entering the App Check Debug Token from the iOS simulator into the debug token section of the App Check for the second database, I am repeatedly getting this error upon querying data from the second database:

[cloud_firestore/permission-denied] The caller does not have permission to execute the specified operation.

Retrieving data from the first app-check enforced database is totally fine.

I'm using App Attest, iOS simulator is running on iOS 16.2 iPhone 14 Pro.

Reproducible code sample, within flutter:

Future<void> main() async {
await Firebase.initializeApp(
     name: "Default",
     options: DefaultFirebaseOptions.currentPlatform,
   );



 if (defaultTargetPlatform == TargetPlatform.android) {
   await Firebase.initializeApp(
     //Android app ID's from firebase console of project 2
       name: 'secondApp',
       options: const FirebaseOptions(
         apiKey: '',
         appId: '',
         messagingSenderId: '',
         projectId: '',
         storageBucket: '',
       ));
 } else if (defaultTargetPlatform == TargetPlatform.iOS) {
   await Firebase.initializeApp(
     //iOS app ID's from firebase console of project 2
       name: 'secondApp',
       options: const FirebaseOptions(
         apiKey: '',
         appId: '',
         messagingSenderId: '',
         projectId: '',
         storageBucket: '',
       ));
 }

 await FirebaseAppCheck.instance.activate(

 );

}

My separate instance that I'm using to communicate to the second app-check enforced database:

  final FirebaseFirestore _firestore2 = FirebaseFirestore.instanceFor(app: Firebase.app('secondApp'));

I have left out the relevant details for the initialisation of the second database. As you can see, app check is activated.

Firebase security rules for second app-check enforced database:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /products/{documents=**} {
           allow read
    }
  }
}

When App Check is not enforced for the second database I am able to communicate with the second database.

Is it possible to use a single App Check instance for 2 separate Cloud Firestore databases that a Flutter App connects to?

nialljawad
  • 23
  • 1
  • 4
  • That should be possible as long as you initialize a separate `FirebaseApp` correctly for the second project, and then get the `Firestore` instance from that. Can you edit your question to show the [minimal code with which anyone can reproduce the problem?)[http://stackoverflow.com/help/mcve] – Frank van Puffelen Apr 02 '23 at 22:01
  • can you check stackoverflow [link1](https://stackoverflow.com/questions/54479483/get-error-firestore-the-caller-does-not-have-permission-to-execute-the-specif) & [link2](https://stackoverflow.com/questions/73414933/cloud-firestore-permission-denied-the-caller-does-not-have-permission-to-execute) which might help – Sathi Aiswarya Apr 03 '23 at 07:25
  • @FrankvanPuffelen Isn't the FirebaseAppCheck a separate instance entirely, unconnected to the Firestore instance? I am making my calls to the second database using this separate instance in any case. I am able to read from the second app-check enforced database using a separate React CMS, which suggests to me that the problem is using app check. I have updated my question. – nialljawad Apr 03 '23 at 09:46
  • @SathiAiswarya this didn't help. I am able to read from the second app-check enforced database using a separate React CMS, which suggests to me that the problem is using app check. – nialljawad Apr 03 '23 at 09:48
  • Have you initialized app check for both apps also have you enforced the second database in Firebase console and Instead of initializing sdk in main function you can create FirebaseOptions2.dart file which have initialization of second app – Sathi Aiswarya Apr 03 '23 at 13:12
  • @SathiAiswarya Yes I have also tried initialising App Check for both apps. Yes I have enforced the second database in the Firebase Console. I have a second firebase_options for the second database, but why would not initialising it in the main() make a difference to App Check? Are you able to reproduce my error? Thanks for your help. – nialljawad Apr 03 '23 at 13:53
  • @SathiAiswarya the separate React CMS app that I am using to connect to the second database has App-Check integration, just for reference. No problems with that one. Which suggests an App Check problem on the Flutter end. Just to re-iterate, i am able to make requests with the Flutter app with the first database, just not the second. – nialljawad Apr 03 '23 at 14:00

0 Answers0