3

I am using Firebase in my app and I've noticed when I am actively making changes and LiveSync updates the app it will sometimes say "firebase.init error: Firebase already initialized". This happens when the changes don't trigger a whole application restart (ex. an html file). It completely messes up my current authentication state and forces me to restart the app anyways.

Is there some way I can catch for this happening or prevent it? I can try to make a demo app for it, but I feel this might have happened to somebody already.

I am just using the standard firebase.init as shown in the documentation in my app.component, nothing special or different.

Narendra
  • 4,514
  • 2
  • 21
  • 38
keerl
  • 116
  • 1
  • 11

1 Answers1

1

Instead of using on ngOnit of App.component, try to initlize firebase on app launch event.

applicationOn(launchEvent, (args: LaunchEventData) => {
    firebase.init({
   // Optionally pass in properties for database, authentication and cloud messaging,
  // see their respective docs.
  }).then(
function () {
  console.log("firebase.init done");
},
function (error) {
  console.log("firebase.init error: " + error);
}
 );
});
Narendra
  • 4,514
  • 2
  • 21
  • 38
  • Since I have to run this code in main.ts I do not have access to my services because the app hasn't started, which are what handles the user being logged in and out. – keerl Mar 15 '19 at 23:16
  • 2
    I was able to achieve this by using a detached onAuthStateChanged in app.component `firebase.addAuthStateListener(listener)` and with testing if firebase.initialized is true, then I manually get the current user with firebase.getCurrentUser(). Round-about way, but it works. – keerl Mar 15 '19 at 23:35