Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change Firebase project at runtime #653

Open
robingenz opened this issue Jun 27, 2024 · 7 comments
Open

feat: change Firebase project at runtime #653

robingenz opened this issue Jun 27, 2024 · 7 comments

Comments

@robingenz
Copy link
Member

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

@emmernme
Copy link

emmernme commented Aug 1, 2024

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
Copy link

I second this!

@emmernme
Copy link

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
Copy link
Member Author

robingenz commented Aug 24, 2024

@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
Copy link

emmernme commented Aug 30, 2024

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
Copy link
Member Author

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants