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.

3
  • 5
    As far as I know you can only be authenticated once in a single page that uses Firebase authentication. From this page (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. Aug 19, 2014 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, 2014 at 22:08
  • 1
    Both your connections are to the same Firebase: https://developer-api.nest.com. One Firebase => one authentication state. Aug 20, 2014 at 1:19

2 Answers 2

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.

4
  • 1
    Yeah, thank you. So it's not possible to do what I want to do this way, so I've started working on the same thing but using REST.
    – c4b4d4
    Aug 20, 2014 at 21:25
  • 1
    Yeah, it looks like that might work. At least the docs don't provide any hints about the REST API being stateful (on client or server). Aug 20, 2014 at 21:27
  • This is an Example of bad library design. Apr 8, 2016 at 7:53
  • I think they deliberately did it to prevent people from misusing by connecting to multiple firebase databases to remain within the free tier. Jan 26, 2017 at 8:16
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); });

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.