对firebase的多个自定义认证请求失败 [英] Multiple Custom authentication requests to firebase failing

查看:210
本文介绍了对firebase的多个自定义认证请求失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在尝试获取多个firebase引用时,只有最后一个请求回调被调用。

  console.log('authentating Users'); 
var firebaseRef1 = new Firebase('https://stckflw.firebaseio.com/Users');
firebaseRef1.authWithCustomToken('< token>',function(error,authData){
console.log('authentication callback for Users');
}
console.log('authenticating Messages');
var firebaseRef2 = new Firebase('https://stckflw.firebaseio.com/Messages');
firebaseRef2.authWithCustomToken('< token>',function(error,authData){
console.log('authentication callback for Messages');
}
console.log('authenticating Emails');
var firebaseRef3 = new Firebase('https://stckflw.firebaseio.com/Emails');
firebaseRef3.authWithCustomToken('< token>',function(error,authData){
console.log('authentication callback for Emails');
});

我看到这样的日志



验证用户



验证电子邮件的验证回调



我希望在认证时一个接一个地获得所有3个回调,所以我希望看到



验证用户



验证用户验证回电
认证回调邮件
电子邮件的认证回调



我缺少此处发生的原因吗?



我想实现它,所有的回调都会在身份验证时触发,而不会丢失任何回调。



我在这里创建了一个示例 http://jsfiddle.net/aniruddhbk/rvkz9mrt/4/

解决方案

由于身份验证是异步发生的,因此这些调用在在平行下。当您开始新的身份验证呼叫时,它会自动取消现有身份验证呼叫。如果不了解您的使用情况,您可以在创建 Firebase 时通过传递一个额外的(未记录的)参数启动每个Firebase在自己的上下文/ code>参考:

  console.log('authentating Users'); 
var firebaseRef1 = new Firebase('https://stckflw.firebaseio.com/Users','Users');
firebaseRef1.authWithCustomToken('< token>',function(error,authData){
console.log('authentication callback for Users');
}
console.log('authenticating Messages');
var firebaseRef2 = new Firebase('https://stckflw.firebaseio.com/Messages','Messages');
firebaseRef2.authWithCustomToken('< token>',function(error,authData){
console.log('authentication callback for Messages');
}
console.log('authenticating Emails');
var firebaseRef3 = new Firebase('https://stckflw.firebaseio.com/Emails','Emails');
firebaseRef3.authWithCustomToken('< token>',function(error,authData){
console.log('authentication callback for Emails');
});

这将允许每个调用完成,并在一个Firebase客户端中提供三个并发的验证会话。

 验证用户
验证消息
验证电子邮件
用户验证回调
消息的认证回调
电子邮件的认证回调

Kato说: 执行此操作,并找到将所有三个会话的权限合并为单个令牌/会话的方法。


When trying to get multiple firebase references one after the other, only the last requests callbacks gets called.

Below I am trying to get 3 firebase references for different data

console.log('authenticating Users');
var firebaseRef1 = new Firebase('https://stckflw.firebaseio.com/Users');
firebaseRef1.authWithCustomToken('<token>',function(error, authData){
    console.log('authentication callback for Users');
} );
console.log('authenticating Messages');
var firebaseRef2 = new Firebase('https://stckflw.firebaseio.com/Messages');
firebaseRef2.authWithCustomToken('<token>',function(error, authData){
    console.log('authentication callback for Messages');
} );
console.log('authenticating Emails');
var firebaseRef3 = new Firebase('https://stckflw.firebaseio.com/Emails');
firebaseRef3.authWithCustomToken('<token>',function(error, authData){
    console.log('authentication callback for Emails');
} );

I am seeing the log like this

authenticating Users
authenticating Messages
authenticating Emails

authentication callback for Emails

Whereas I expect to get all 3 callbacks one after another on authentication, so I expect to see logs like

authenticating Users
authenticating Messages
authenticating Emails

authentication callback for Users
authentication callback for Messages
authentication callback for Emails

Am I missing something here cause of which this happens?

I want to achieve it in such a way that all the callback gets triggered on authentication, without missing any.

I have created a example here http://jsfiddle.net/aniruddhbk/rvkz9mrt/4/

解决方案

Since authentication happens asynchronously, these calls are not executing after each other, but mostly in parallel. When you start a new authentication call it automatically cancels the existing one. Without understanding much about your use-case, you can start each Firebase in its own context/session, by passing an extra (undocumented) parameter in when you create the Firebase reference:

console.log('authenticating Users');
var firebaseRef1 = new Firebase('https://stckflw.firebaseio.com/Users', 'Users');
firebaseRef1.authWithCustomToken('<token>',function(error, authData){
    console.log('authentication callback for Users');
} );
console.log('authenticating Messages');
var firebaseRef2 = new Firebase('https://stckflw.firebaseio.com/Messages', 'Messages');
firebaseRef2.authWithCustomToken('<token>',function(error, authData){
    console.log('authentication callback for Messages');
} );
console.log('authenticating Emails');
var firebaseRef3 = new Firebase('https://stckflw.firebaseio.com/Emails', 'Emails');
firebaseRef3.authWithCustomToken('<token>',function(error, authData){
    console.log('authentication callback for Emails');
} );

This will allow each call to complete and gives you three concurrent authenticated sessions in one Firebase client.

authenticating Users
authenticating Messages
authenticating Emails
authentication callback for Users
authentication callback for Messages
authentication callback for Emails

As Kato said: it is probably better to not do this and find a way to combine the permissions for all three sessions into a single token/session.

这篇关于对firebase的多个自定义认证请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆