Best practice to init firebase
See original GitHub issueHi guys,
I am trying to get what is the right way to init firebase (I am working on Angular project).
In the beginning I’ve put the firebase.init()
in the main.ts
but after I started to work on IOS I didn’t received a background messaging on IOS so the thing that did the work for me was or to put the firebase.init()
on 1000ms timeout on main.ts
or to put it on the first page that I route to.
The thing is that I want to make the user login as he open the app, like this:
firebase.getAuthToken({ forceRefresh: false }).then(token => {
if (!token) {
return;
}
this._loginUtils.loginFromToken(token);
}, err => {
firebase.logout();
console.log("There is no token saved in the device");
});
Now, the thing is that the setTimeout
solution is an ugly solution, but it works just fine, but when I implements the “prettier” solution of putting the init
function on the first page ngOnInit
function the init function never resolved, and I’ll explain:
This is the “prettier” solution (exactly as @EddyVerbruggen wrote in the demo):
ngOnInit(): void {
firebase.init({
onAuthStateChanged: function (data) { // optional but useful to immediately re-logon the user when he re-visits your app
console.log(data.loggedIn ? "Logged in to firebase" : "Logged out from firebase");
if (data.loggedIn) {
console.log("user's email address: " + (data.user.email ? data.user.email : "N/A"));
}
}
}).then(() => {
firebase.getAuthToken({ forceRefresh: false }).then(token => {
if (!token) {
return;
}
this._loginUtils.loginFromToken(token);
}, err => {
firebase.logout();
console.log("There is no token saved in the device");
});
})
}
The buggy issue here is that the init
function never resolved.
After debug the actual code of init
function in firebase
module in those two situation I found out that the following:
-
when putting the init inside the first component in looks like it gets to this line
appModule.on(appModule.launchEvent, runInit);
but it seems like theappModule.launchEvent
never raise or already raised, so the firebase init never happen and it is never resolved, so the use cannot automatically login into the app. -
when putting it in the
main.ts
withsetTimeout
it looks like it gets to this linerunInit();
and all works just fine but this is a very ugly and bad performance solution.
PLS! What is the right way to init the firebase?
Thanks a lot 😃
Issue Analytics
- State:
- Created 6 years ago
- Comments:22 (8 by maintainers)
Top GitHub Comments
Yeah got that one as well and fixed it locally.
Better yet I found the culprit with NativeScript 3.4 and this plugin on Android: https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/f07762ddea6d0760f2e55ce2703ad652cf74bf4c/src/firebase.android.ts#L353
The
foregroundActivity
used to be defined in NativeScript < 3.4 when your OnInit function runs, but that’s no longer the case. This fact also bit me in 3 other plugins unfortunately and I didn’t realise this one was affected as well.Fix inbound!
Updating the embedded demo-ng app to 5.2.0 and will see what
ngOnInit
inapp.component
does.