Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angularjs – Firebase $authWithOAuthRedirect在没有页面刷新的情况下不会调用$onAuth大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个简单的Firebase Facebook OAuth登录设置,如下面official tutorial for Ionic Firebase Facebook登录.

问题是,一旦我点击登录与Facebook按钮,我被重定向到Facebook,我登录,然后我被重定向回我的应用程序.但问题是我留在登录页面.奇怪的是,如果我通过按F5来物理刷新我的浏览器(使用离子服务测试)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秒的延迟几乎不够(我发现建议500ms的资源很少就足够了,但在我的情况下,情况并非如此).

解决方法

发表在github上的问题,但也会在这里回复.

这只发生在使用$authWithOAuthRedirect的浏览器中.该示例适用于cordova应用程序,因此这不应该是一个问题.

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

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

大佬总结

以上是大佬教程为你收集整理的angularjs – Firebase $authWithOAuthRedirect在没有页面刷新的情况下不会调用$onAuth全部内容,希望文章能够帮你解决angularjs – Firebase $authWithOAuthRedirect在没有页面刷新的情况下不会调用$onAuth所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。