capawesome-team / capacitor-firebase

⚡️ Firebase plugins for Capacitor. Supports Android, iOS and the Web.
https://capawesome.io/plugins/firebase/
Apache License 2.0
380 stars 98 forks source link

feat: change Firebase project at runtime #653

Open robingenz opened 2 months ago

robingenz commented 2 months ago

Allow changing the firebase project to runtime, see https://stackoverflow.com/a/41305288/6731412

emmernme commented 1 month ago

This would be great for apps supporting multiple languages with different Firebase projects for each language. We have an app built this way at the moment, which we are not able to deploy Firebase Authentication to until this is supported.

sigveholmen commented 1 month ago

I second this!

emmernme commented 1 month ago

I have made an implementation for this for the Authentication Plugin, see the fork here: https://github.com/emmernme/capacitor-firebase/tree/project-switching

Currently tested for iOS and Android, and seems to work as expected!

Usage example:

  /**
   * Configures Capacitor Firebase app for the given country, if not already initialized, and sets that as the current active app
   * @param country
   * @private
   */
  private async useFirebaseApp(country: "no" | "se" | "dk") {
    if (country != "no" && !(await FirebaseAuthentication.firebaseAppIsInitialized({name: country})).result) {
      const firebaseConfig = environment.firebase[country];
      await FirebaseAuthentication.initWithFirebaseConfig({ name: country, config: firebaseConfig });
    }
    await FirebaseAuthentication.useFirebaseApp({ name: (country == "no") ? "default" : country });
  }

After the apps have been initialized and useFirebaseApp has been called, the native implementation will use the current selected app for all other calls.

@robingenz Let me know if you have any further requirements before we make a PR for this issue, it's a simple but effective solution at least :)

robingenz commented 1 month ago

@emmernme I like the approach. However, I would not add this code to the Capacitor Firebase Authentication plugin, but to the Capacitor Firebase App plugin (except for the getFirebaseAppInstance and getApp methods). Feel free to create a PR. I would also rename the methods slightly.

emmernme commented 3 weeks ago

Thanks! I think it does need to be in each of the plugins too, in order to actually use the selected app, unless the plugins communicate with each other at all on the native side? I'm not sure how we could implement this in the Firebase App-plugin and still use it with Firebase Authentication otherwise. Also, I think it's a good approach to allow using different projects for the different plugins, as this covers our specific use case and some others that I've researched :)

robingenz commented 3 weeks ago

I think it does need to be in each of the plugins too, in order to actually use the selected app, unless the plugins communicate with each other at all on the native side?

Yes, but only the methods for selecting the Firebase project. The initialization of the Firebase app should be done by the Firebase app plugin.

emmernme commented 3 weeks ago

Good point – I'll make the changes and a PR!