2

I have installed and run firebase admin on my node server, it has been running well until today, there is no given error to tell what happened, it simply stops working.

var admin = require("firebase-admin");

            var serviceAccount = require("mycom-firebase-adminsdk-d1ebt123456.json");

            var app = FireBaseAdapter.admin.initializeApp({
                credential: FireBaseAdapter.admin.credential.cert(serviceAccount),
                databaseURL: "https://xxxx.firebaseio.com"
            });

var db = app.database();
var ref = db.ref("messages"); // this node actually exists in my db.


ref.on("value", function(snapshot) {
// this will never be called - which has been working before.

  console.log(snapshot.val());
}, function (errorObject) {
  console.log("The read failed: " + errorObject.code);
});

I even wrap it in the try catch to print out the error, but there isn't any. I can log into the firebase console in the account and see my database with no change (small database) (although it seems be slower than normal). Is there something wrong with firebase Admin SDK? Any help is appreciated.

Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39
  • I just quickly checked and can still read from one of my databases with the admin credentials, similar to how your code does it. I tried with both version 4.0.0 and 4.0.4. So I don't think there is a general failure anywhere and an immediately obvious bug in the admin SDK. – Frank van Puffelen Dec 11 '16 at 09:09
  • It suddenly stop working, it still connects to db somehow, but the "on value" event no longer fires. There is no error to troubleshoot though. – Telvin Nguyen Dec 11 '16 at 12:55

1 Answers1

4

After spending many hours to find out the cause, I found the problem. Since I couldn't find any way to enable log of firebase-admin, so it was the dead end to troubleshoot the issue while everything runs silently, so I switched to use the firebase package to have the logging

var firebase = require("firebase");
firebase.initializeApp({
    databaseURL: "https://xxxxx.firebaseio.com",
    serviceAccount: '....'
});

firebase.database.enableLogging(true); // <=== important

My issue is quite similar to this question

then I could see the actual error:

p:0: Failed to get token: Error: Error refreshing access token: invalid_grant (Invalid JWT: Token must be a short-lived token and in a reasonable timeframe)

The solution for this issue was explained on this anwser. This issue caused by a poor synchronisation of the computer's clock where the code was executed that had a lag of 5 minutes (due to a faulty battery for the internal clock). It started working again when I manually changed the internal time of my computer to the correct one (or totally I reset my computer date-time).

In my case, after resetting the datetime&timezone, the firebase automatically works again and I do not need to re-generate another service account.

Community
  • 1
  • 1
Telvin Nguyen
  • 3,569
  • 4
  • 25
  • 39