Real Tech Hacks
Real Tech Hacks

Reputation: 367

How to use two firebase projects in single Android app at same time?

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...

Upvotes: 1

Views: 1977

Answers (3)

Venkat
Venkat

Reputation: 404

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.

Upvotes: 2

l1b3rty
l1b3rty

Reputation: 3660

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.

Upvotes: 0

Jake Lee
Jake Lee

Reputation: 7989

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} {
            // ...
         }
     }
   }
}

Upvotes: 1

Related Questions