Multiple Firebases

I have a problem when I try to create more than one instance of new Firebase(url); .

For instance:

 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 what it is no more.

So this means that it never calls the callback function from v1.auth(); she ignores it, it looks like it is getting overridden by v2.auth(); , even if they are different instances of Firebases, and it interferes with everything else, for example, v1.child("path").on("value",function(snapshot){}); and v2.child("path").on("value",function(snapshot){}); will not work when this happens.

+6
source share
2 answers

You can only be authenticated once on a single page using Firebase authentication.

From this page in the Firebase documentation :

All Firebase links have the same authentication status. Therefore, if you call new Firebase () twice and call auth () on one of them, they will both be authenticated.

Of course, it may be that nest-api authentication works differently, but I doubt it.

Update

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

It is not possible to authenticate with multiple credentials of the same Firebase at the same time, even if we call .auth for different Firebase links. Authentication status is global and applies to all links to Firebase. However, you can create links to two or more different Firebase and authenticate for them independently.

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

+8
source

You can make multiple instances using the new Firebase.Context () as the 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); }); 
0
source

Source: https://habr.com/ru/post/974011/


All Articles