我遇到的问题是当页面加载时需要一秒钟来检查用户是否登录。我使用该auth函数来创建我已经成功的currentUser变量用于由点击事件触发的其他功能。但是在加载页面时加载特定于用户的东西时,它表示该变量未定义(因为它需要身份验证才能完成)。等待auth使用firebase加载的问题
如何在auth完成之前获取当前用户,或者让加载时触发的函数等待auth被授权?
我遇到的问题是当页面加载时需要一秒钟来检查用户是否登录。我使用该auth函数来创建我已经成功的currentUser变量用于由点击事件触发的其他功能。但是在加载页面时加载特定于用户的东西时,它表示该变量未定义(因为它需要身份验证才能完成)。等待auth使用firebase加载的问题
如何在auth完成之前获取当前用户,或者让加载时触发的函数等待auth被授权?
火力地堡发布新的客户端库上周五(2014年10月3日),其中包括用于验证数据的同步访问器,这听起来正如您所需:
var ref = new Firebase('https://<your-firebase>.firebaseio.com');
var authData = ref.getAuth();
尝试将您的客户端库升级到v1.1.0或更高版本的Web,并参阅https://www.firebase.com/docs/web/guide/user-auth.html#section-monitoring-authentication的最新文档。
您可以使用setInterval
,下面的代码将检查currentUser
每半秒和运行fireBusinessLogicHereFunction()
时currentUser
将可用:
var checkAuthInterval = setInterval(function(){
if (typeof currentUser !== undefined) {
clearInterval(checkAuthInterval);
fireBusinessLogicHereFunction();
}
}, 500);
以后将有可能使用promises
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise