question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best practice to init firebase

See original GitHub issue

Hi 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 the appModule.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 with setTimeout it looks like it gets to this line runInit(); 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:closed
  • Created 6 years ago
  • Comments:22 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
EddyVerbruggencommented, Jan 23, 2018

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!

2reactions
EddyVerbruggencommented, Jan 23, 2018

Updating the embedded demo-ng app to 5.2.0 and will see what ngOnInit in app.component does.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Firebase to your JavaScript project - Google
Step 1: Create a Firebase project and register your app · Step 2: Install the SDK and initialize Firebase · Step 3: Access...
Read more >
Firebase Cloud Functions Best Practice? - Stack Overflow
Initializing the admin SDK from an organized and dedicated directory can be a good idea: Do it and then import it to your...
Read more >
10 Firebase Hosting Best Practices - CLIMB
10 Firebase Hosting Best Practices · 1. Enable HTTPS for all sites · 2. Set up custom domain names · 3. Configure redirects...
Read more >
The Ultimate Guide To Firebase With Next.JS - Jarrod Watts
Next.js is a way of building awesome React applications, best known for ... Here we are simply initializing Firebase with our configuration, ...
Read more >
Learn Dev Config Best Practices – Firebase Fundamentals
So first of, security, like I said, advising a DELETE request to that database, everything gone, and that's not good. Also the code...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found