【问题标题】:issues with waiting for auth to load using firebase使用 firebase 等待身份验证加载的问题
【发布时间】:2014-10-05 19:45:53
【问题描述】:

我遇到的问题是,当页面加载时,需要一秒钟来检查用户是否已登录。我使用该 auth 函数来创建 currentUser 变量,该变量已成功用于由单击触发的其他函数中事件。但是,在页面加载时加载特定于该用户的内容时,它表示变量未定义(因为它需要验证才能完成)。

如何在身份验证完成之前获取当前用户,或者让加载时触发的函数等待身份验证完成?

【问题讨论】:

    标签: javascript jquery firebase


    【解决方案1】:

    Firebase 上周五(2014 年 10 月 3 日)发布了新的客户端库,其中包括用于身份验证数据的同步访问器,这听起来正是您所需要的:

    var ref = new Firebase('https://<your-firebase>.firebaseio.com');
    var authData = ref.getAuth();
    

    尝试将您的客户端库升级到网络版 v1.1.0 或更高版本,并在 https://www.firebase.com/docs/web/guide/user-auth.html#section-monitoring-authentication 上查看最新文档。

    【讨论】:

      【解决方案2】:

      您可以使用setInterval,以下代码将每半秒检查一次currentUser,并在currentUser可用时运行fireBusinessLogicHereFunction()

      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

      【讨论】:

        猜你喜欢
        • 2020-03-06
        • 1970-01-01
        • 1970-01-01
        • 2021-04-06
        • 1970-01-01
        • 1970-01-01
        • 2016-11-15
        • 2017-03-16
        • 2021-12-09
        相关资源
        最近更新 更多