Search code examples
node.jsauthenticationfirebasenest-api

Multiple Firebases instances


I'm having a problem when I try to make more than one new Firebase(url); instance.

For example:

var token1 = "c.AdASdsAsds...";
var token2 = "c.dkasdEddss...";

var v1 = new Firebase('https://developer-api.nest.com');
v1.auth(token1,function(err){ 
    console.log("Connected 1 "+err);
},function(err){ console.log("Cancel 1: "+err); });

var v2 = new Firebase('https://developer-api.nest.com');
v2.auth(token2,function(err){ 
    console.log("Connected 2 "+err);
},function(err){ console.log("Cancel 2 "+err); });

Console logs: Connected 2 null and that's it.. no more.

So, it means it never calls the callback function from v1.auth(); it ignores it, it looks like it becomes overridden by the v2.auth(); even though they are different Firebases instances, and it interferes everything else, for example, the v1.child("path").on("value",function(snapshot){}); and v2.child("path").on("value",function(snapshot){}); won't work when this happens.


Solution

  • You can only be authenticated once in a single page that uses Firebase authentication.

    From this page in the Firebase documentation:

    All references to a Firebase share the same authentication status. So if you call new Firebase() twice and call auth() on one of them, they will both be authenticated.

    It could of course be that the nest-api authentication works differently, but I doubt it.

    Update

    You provided a quote from another page in the Firebase documentation:

    It is not possible to authenticate with multiple credentials to the same Firebase simultaneously, even if we call .auth on different Firebase references. Authentication state is global and applies to all references to the Firebase. However, it is possible to create references to two or more different Firebases and authenticate to those independently.

    But in your code both connections are to the same Firebase: https://developer-api.nest.com. One Firebase => one authentication state.