【问题标题】:Firebase $authWithOAuthRedirect doesn't call $onAuth without page refreshFirebase $authWithOAuthRedirect 在没有页面刷新的情况下不会调用 $onAuth
【发布时间】:2015-10-30 14:46:16
【问题描述】:

我有一个简单的 Firebase Facebook OAuth 登录设置,如下所示,按照 official tutorial 进行 Ionic Firebase Facebook 登录。

问题是,一旦我点击使用 Facebook 登录按钮,我会被重定向到 Facebook,我登录,然后我会被重定向回我的应用程序。但是,问题是我留在登录页面。奇怪的是,如果我通过按 F5 物理刷新浏览器(使用 ionic serve 进行测试),onAuth 会触发,我会被重定向到 Items 页面。如果我不刷新浏览器,onAuth 就不会被调用。

有人遇到过这个问题吗?你是怎么解决的?

请注意,是的,我做了我的研究(并且开始有点失去它),但无法让它发挥作用。我搜索了 SO,尝试使用来自 google 群组的$timeout,尝试调用 $scope.$apply(),但都无济于事 - 所以请帮我找出我做错了什么?

.controller('AppCtrl', function($scope, Items, Auth, $state, $ionicHistory) {
    $scope.login = function(provider) {
        Auth.$authWithOAuthRedirect(provider).then(function(authData) {

        }).catch(function(error) {
            if (error.code === "TRANSPORT_UNAVAILABLE") {
                Auth.$authWithOAuthPopup(provider).then(function(authData) {
                    
                });
            } else {
                console.log(error);
            }
        });
    };

    $scope.logout = function() {
        Auth.$unauth();
        $scope.authData = null;

        $window.location.reload(true);
    };

    Auth.$onAuth(function(authData) {
        if (authData === null) {
            console.log("Not logged in yet");
            $state.go('app.login');
        } else {
            console.log("Logged in as", authData.uid);
            
            $ionicHistory.nextViewOptions({
                disableBack: true
              });
            $state.go('app.items');
        }
        $scope.authData = authData; // This will display the user's name in our view
    });
})
<ion-view view-title="Members area">
    <ion-content padding="true">
        <div ng-if="!authData">
            <button class="button button-positive button-block" ng-click="login('facebook')">Log In with Facebook</button>  
        </div>       
        
        <div ng-if="authData.facebook">
            <div class="card">
                <div class="item item-text-wrap">Hello {{authData.facebook.displayName}}!</div>                
            </div>

            <button class="button button-assertive button-block" ng-click="logout()">Log out</button>
        </div>        

    </ion-content>
</ion-view>

编辑:我通过使用 $timeout 解决了它:

$timeout(function(){
    Auth.$onAuth(function(authData) {
        if (authData === null) {
            console.log("Not logged in yet");
            $state.go('app.login');
        } else {
            console.log("Logged in as", authData.uid);

            $ionicHistory.nextViewOptions({
                disableBack: true
              });
            $state.go('app.items');
        }
        $scope.authData = authData; // This will display the user's name in our view
    });
}, 3000);

但是,这感觉不对(委婉地说),必须有更好的方法,所以请提出一个建议。此外,我注意到惊人的 3 秒延迟几乎不够(我发现很少有资源建议 500 毫秒就足够了,但就我而言,情况并非如此)。

【问题讨论】:

    标签: angularjs ionic-framework ionic firebase angularfire


    【解决方案1】:

    已在github问题上发表,但也会在这里回复。

    这只发生在使用$authWithOAuthRedirect 的浏览器中。该示例适用于科尔多瓦应用程序,因此这确实不应该成为问题。

    但如果你真的需要它,你可以跳过重定向并使用弹出窗口。

        Auth.$authWithOAuthPopup(authMethod).then(function(authData) {
          console.dir(authData);
        });
    

    【讨论】:

    • 啊!非常感谢!我一定在文档中错过了这个:/。我认为它实际上被覆盖在 catch 块中,但似乎情况正好相反。无论如何,谢谢,我只想亲自感谢您提供的出色框架!继续努力!
    【解决方案2】:

    这是一个已在最新的 Firebase JS 客户端版本 (2.3.0) 中修复的错误。您现在可以使用$authWithOAuthRedirect,当您返回页面时将触发 $onAuth 回调。

    【讨论】:

    • 嘿 Sara,感谢您提供更新和修复。继续在如此出色的 Firebase 上继续努力!
    • 不,还是坏了。
    • 这方面有什么进展吗?
    猜你喜欢
    • 2020-08-29
    • 2017-05-11
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 2016-04-22
    • 2016-09-02
    相关资源
    最近更新 更多