6

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.

c4b4d4
  • 964
  • 12
  • 32
  • 5
    As far as I know you can only be authenticated once in a single page that uses Firebase authentication. From this page (https://www.firebase.com/docs/web/api/firebase/auth.html): "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. – Frank van Puffelen Aug 19 '14 at 14:58
  • @FrankvanPuffelen Yeah it does say that, but I guess it's talking about all references to an specific Firebase. On another link it says: _"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.**"_ That's what I want to achieve, but I haven't figured out how. – c4b4d4 Aug 19 '14 at 22:08
  • 1
    Both your connections are to the same Firebase: `https://developer-api.nest.com`. One Firebase => one authentication state. – Frank van Puffelen Aug 20 '14 at 01:19

2 Answers2

9

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.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
1

You can make multiple instances using new Firebase.Context() as second parameter!

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

    var v1 = new Firebase('https://developer-api.nest.com', new Firebase.Context());

    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', new Firebase.Context());

    v2.auth(token2,function(err){ 
        console.log("Connected 2 "+err);
    },function(err){ console.log("Cancel 2 "+err); });