prescottprue / react-redux-firebase

Redux bindings for Firebase. Includes React Hooks and Higher Order Components.
https://react-redux-firebase.com
MIT License
2.55k stars 559 forks source link

Connecting and disconnecting to a database dynamically #1001

Open orenk86 opened 3 years ago

orenk86 commented 3 years ago

Hello, I'm raising an issue that was already discussed here some time ago: https://github.com/prescottprue/react-redux-firebase/issues/70

In our app we have only some specific pages in which a connection to Firebase is needed, and we prefer to be connected to the database only when needed. Also due to security requirements in our organization, we use a custom authentication system in which upon entering a relevant page, our backend generates a specific token with appropriate permissions for the path we need to connect to and the user that is currently logged in (different users/roles have different permissions and can access different paths). Only then, we can call firebase.initializeApp(config) to connect. Of course, upon exiting the page we need to disconnect, and then generate a new token and reconnect when entering another relevant page (because each token allows access to a specific path and not to the entire DB).

For all this, we need the ability to initialize our app, wrapped in ReactReduxFirebaseProvider without providing an API key, and call firebase.initializeApp(config) at a later stage when we have the API key.

This works for us in version 3.0.0-alpha.3 (perhaps by accident?), but not in later versions including the official release of 3.0.0. The app crashes on loading with this error: Firebase: No Firebase App '[DEFAULT]' has been created - call Firebase App.initializeApp() (app/no-app) Seems like I need to stay with 3.0.0-alpha.3 for now, until a solution is introduced.

Is there any way we can achieve this?

Ideally in the future we would like to have the entire configuration fetched from the backend and loaded dynamically so we can have full flexibility, as we only connect to the database when we need to, and we might decide to split the data across multiple databases. But this can be at a later stage, and I can open a new issue when it becomes relevant.

Thanks!!

prescottprue commented 3 years ago

Really great to know what version things work in. I'll try to reproduce, but would it be possible for you to provide an example repo so I can be sure to be handling your use case? Not sure of a change that would have caused this break, but I'll look through the git history and see if anything sticks out