NodeJs firebase server Authentication with admin privileges

584 views
Skip to first unread message

Tene Patrice

unread,
Jul 17, 2016, 1:03:04 PM7/17/16
to Firebase Google Group

I am trying to setup firebase server using nodejs as discussed Here. I have followed every steps from creating a project and downloading the .json file to changing the rules of the database but it still doesn't seem to work.


My code is not able to read or write data from/to any tables using admin privileges even the ones with public read and write access. This is my code from the server side:


var firebase = require('firebase');

firebase.database.enableLogging(true);

firebase.initializeApp({
serviceAccount: "./App.json",
databaseURL: "https://----.firebaseio.com"
});
//
var db = firebase.database();
var ref = db.ref("testPosts");

ref.once("value", function(snapshot){
    console.log(snapshot.val());
}, function(error){
    console.log(error);
});


The code below is how the security is set on the server:

"rules": {
  "users": {
    "$uid": {
      ".read": "auth != null && auth.uid == $uid",
      ".write": "auth != null && auth.uid == $uid"
    }
  },
"testPosts": {
    ".read": "auth != null",
    ".write": "auth != null"
  }
}


When I run the code I get the following error:

p:0: Listen called for /posts default
p:0: Making a connection attempt
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)
p:0: data client disconnected
p:0: Trying to reconnect in 77.64995932509191ms
0: onDisconnectEvents


Can someone please help me with this? I have been struggling with it for a while now.




Jacob Wenger

unread,
Jul 17, 2016, 10:00:23 PM7/17/16
to fireba...@googlegroups.com
Hey Tene,

Sorry you are having issues. Let's see if we can get your sorted out.

First, make sure you are using the latest version of the Firebase Node.js library. The latest can be found here and (as of this message) is 3.2.0.

Next, make sure the service account JSON key file you are using corresponds to the same Firebase project as the databaseURL you specify in the SDK configuration.

Next, make sure the service account you created for your Firebase project has the correct permissions. It should be at least an Editor.

Finally, make sure the key JSON file you loaded has not been deleted.

If you are still having issues after trying all that, let us know what version of Node.js you are using, what your Firebase project ID is, and send us the full logging output from Firebase. If you'd prefer, you can send that to me privately and we can just report back here with the resolution once we get it.

Cheers,
Jacob

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/677bdbb0-8809-4fe1-a502-77feef402d52%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tene Patrice

unread,
Jul 18, 2016, 4:12:45 PM7/18/16
to Firebase Google Group
Hi Jacob


As per your request, I am using nodejs version 5.9.1, My project ID is mobiatwork-91b4e and the following is the full logging output from firebase.



C:\Users\PT\Documents\Visual Studio 2015\Projects\Tene-Enterprises\Yenza Life\We
bApp\angular-seed>node --version
v5.9.1

C:\Users\PT\Documents\Visual Studio 2015\Projects\Tene-Enterprises\Yenza Life\We
bApp\angular-seed>node server.js
p:0: Browser went online.
p:0: Listen called for /posts/tasks {"ep":null,"i":"_state","l":1,"sp":null,"vf"
:"l"}
p:0: Listen called for /posts/tasks {"ep":"in_progress","i":"_state","sp":"in_pr
ogress"}
..........................................................................


p:0: Making a connection attempt
p:0: Failed to get token: Error: Error refreshing access token: invalid_grant (I
nvalid JWT: Token must be a short-lived token and in a reasonable timeframe)
p:0: data client disconnected
p:0: Trying to reconnect in 38.48946659825742ms
0: onDisconnectEvents
p:0: Making a connection attempt
p:0: Failed to get token: Error: Error refreshing access token: invalid_grant (I
nvalid JWT: Token must be a short-lived token and in a reasonable timeframe)
p:0: data client disconnected
p:0: Trying to reconnect in 49.58487843070179ms
0: onDisconnectEvents
p:0: Making a connection attempt
p:0: Failed to get token: Error: Error refreshing access token: invalid_grant (I
nvalid JWT: Token must be a short-lived token and in a reasonable timeframe)
p:0: data client disconnected
p:0: Trying to reconnect in 143.73470537667163ms
0: onDisconnectEvents
p:0: Making a connection attempt
p:0: Failed to get token: Error: Error refreshing access token: invalid_grant (I
nvalid JWT: Token must be a short-lived token and in a reasonable timeframe)
p:0: data client disconnected
p:0: Trying to reconnect in 1101.4264577277936ms
0: onDisconnectEvents
p:0: Making a connection attempt
p:0: Failed to get token: Error: Error refreshing access token: invalid_grant (I
nvalid JWT: Token must be a short-lived token and in a reasonable timeframe)
p:0: data client disconnected
p:0: Trying to reconnect in 1401.0464290263828ms
0: onDisconnectEvents
p:0: Making a connection attempt
p:0: Failed to get token: Error: Error refreshing access token: invalid_grant (I
nvalid JWT: Token must be a short-lived token and in a reasonable timeframe)
p:0: data client disconnected
p:0: Trying to reconnect in 899.9756115754764ms
0: onDisconnectEvents
^C

Jacob Wenger

unread,
Jul 22, 2016, 6:01:10 PM7/22/16
to Firebase Google Group
Hmm, I assume you tried all my suggestions (including using version 3.2.0) and nothing resolved the issue, right? I just tried your code on Node 5.12.0 (latest stable) and it worked perfectly fine for my Firebase project.

The invalid_grant error suggests your underlying Google refresh token is invalid. There are a couple reasons this could happen, as noted here. Do either of those scenarios seem to be likely for you? Can you check your server's time and make sure it isn't an NTP issue?

Cheers,
Jacob

Tene Patrice

unread,
Aug 2, 2016, 4:36:00 PM8/2/16
to Firebase Google Group
Thanks so much Jacob. I checked and the problem was the NTP server time.

Problem is fixed
Reply all
Reply to author
Forward
0 new messages