1

I need two firebase projects for single Android app. One project is for users, and other is for Admins. I'm using the admins firebase project for some reasons.

Now I need to connect these two projects in admin app, and I want to add or modify firestore data at a time in both firebase projects from admin app.

How to get instances of two firebase project at single time in Android?

I have seen some stackoverflow questions regarding this but they are not getting two instances at same time.

Can anyone please answer my question...

Real Tech Hacks
  • 367
  • 3
  • 11

3 Answers3

2

Nothing to change your default app. Connect your app to the main firebase project normally. Also, register your app with the second firebase project. Now instead of importing the google-services.json file to the android project directly create a Singleton class like below and replace it with your second firebase project details.

public class MySecondaryProject {
    private static FirebaseApp INSTANCE;

    public static FirebaseApp getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = getSecondProject(context);
        }
        return INSTANCE;
    }

    private static FirebaseApp getSecondProject(Context context) {
        FirebaseOptions options1 = new FirebaseOptions.Builder()
                .setApiKey("AI...gs")
                .setApplicationId("1:5...46")
                .setProjectId("my-project-id")
                // setDatabaseURL(...)      // in case you need firebase Realtime database
                // setStorageBucket(...)    // in case you need firebase storage MySecondaryProject
                .build();

        FirebaseApp.initializeApp(context, options1, "admin");
        return FirebaseApp.getInstance("admin");
    }
}

Now wherever you need this second project instance you can use like below.

  FirebaseFirestore db = FirebaseFirestore.getInstance(); //- DEFAULT PROJECT
  FirebaseFirestore secondDB = FirebaseFirestore.getInstance(MySecondaryProject.getInstance(this)); // - SECOND PROJECT

You must need to follow the singleton pattern to avoid duplicate instances otherwise the app will crash. you can use both the instances simulteneously.

For more details some useful links

I think it will be the perfect answer of your question.

Venkat
  • 384
  • 1
  • 16
1

Short answer: Don't.

Instead of one project for admins and one for users, you want to have role based access control on a single project. For example, admins can delete files, whilst users cannot.

There is a full guide to role based access available in the Firebase docs, along with a few variations. Your rules may end up looking something like:

service cloud.firestore {
   match /databases/{database}/documents {
     match /stories/{story} {
        function isSignedIn() {
          return request.auth != null;
        }

        function getRole(rsc) {
          // Read from the "roles" map in the resource (rsc).
          return rsc.data.roles[request.auth.uid];
        }

        function isOneOfRoles(rsc, array) {
          // Determine if the user is one of an array of roles
          return isSignedIn() && (getRole(rsc) in array);
        }

        function isValidNewStory() {
          // Valid if story does not exist and the new story has the correct owner.
          return resource == null && isOneOfRoles(request.resource, ['owner']);
        }

        // Owners can read, write, and delete stories
        allow write: if isValidNewStory() || isOneOfRoles(resource, ['owner']);

         match /comments/{comment} {
            // ...
         }
     }
   }
}
Jake Lee
  • 7,549
  • 8
  • 45
  • 86
  • Thanks for your response @Jake Lee. I had an idea on implementation of role based users with firebase custom climes. But in my case another project not only for Admins role. It acts as a data backup of admin created app content, and to store some extra confidential details about that specific content. Also for some extra reasons. – Real Tech Hacks Oct 14 '21 at 10:39
  • Nothing you have stated requires a separate project. Security rules & roles handle all the confidentiality - I can state this quite conclusively - I'm doing it right now. – LeadDreamer Oct 21 '21 at 16:04
0

If you only have standard user and admins, just use custom claims. Don't write the the roles to Firestore.

It will make the setup much easier and will not cost you in expensive get calls in your rules.

l1b3rty
  • 3,333
  • 14
  • 32