2014-10-17 35 views
0

我正在构建一个带有用户身份验证的角度+ firebase应用程序(使用angularfire 0.8)。

我需要使用onAuth()auth事件处理程序,因为我将提供多个身份验证路径,包括社交,并希望避免代码重复。在onAuth回调中,我需要将location.path重置为'/'。

通常都工作得很好,但是,如果应用程序被加载在已认证的会话(<F5>,例如),在$scope.$apply()我得到“错误:[$ rootScope:inprog] $已经申请正在进行中“
(如果我不使用$ scope。$ apply(),位置路径不适用于作用域,也不会发生页面更改)。

我怀疑我做了一些愚蠢的错误,但无法确定它...

这是我的工作流程:

app.controller('AuthCtrl', function ($scope, $rootScope, User) { 
    var ref = new Firebase(MY_FIREBASE_URL); 
    $scope.init = function() { 
    $scope.users = []; 
    User.all.$bindTo($scope, 'users').then(function() { 
     console.info('$scope.users bound:', $scope.users); 
    }); 
    }; 
    $scope.login = function() { 
    ref.authWithPassword({ 
     email: $scope.user.email, 
     password: $scope.user.password, 
    }, function(err) { 
     if (err) { 
     console.error('Error during authentication:', err); 
     } 
    }); 
    }; 

    ref.onAuth(function(authData) { 
    if (authData) { 
     console.info('Login success'); 
     var $rootScope.currentUser = $scope.users[authData.uid]; 
     $location.path('/'); 
     $scope.$apply(); 
    } else { 
     console.info('Logout success'); 
    } 
    }); 
}; 

app.factory('User', function ($firebase) { 
    var ref = $firebase(new Firebase(MY_FIREBASE_URL + 'users')); 
    return { 
    all: ref.$asObject() 
    }; 
}); 
+0

嘿MarcoS,我发现你有类似的问题,我的。请你关心看看我的问题:http://stackoverflow.com/questions/31872935/firebase-authwithoauthredirect-doesnt-call-onauth-without-page-refresh – Nikola 2015-08-07 11:33:15

+0

我做到了,但你是一个完全不同的(Ionic/Angular + Firebase)......对不起,我几个月没有使用Angular(只是等待2.0 ... :-),所以我真的不能帮你...: - ( – MarcoS 2015-08-07 14:31:36

+0

感谢您的回复。我设法解决了这个问题。 – Nikola 2015-08-07 16:46:01

回答

1

只是作为参考,我想我后找到的解决方法和我目前采用:

$scope.init = function() { 
    $scope.params = $routeParams; 
    $scope.debug = CFG.DEBUG; 
    $scope.lastBuildDate = lastBuildDate; 
    $scope.error = null; 
    $scope.info = null; 

    $scope.users = []; 

    User.all.$bindTo($scope, 'users').then(function() { 
     // watch authentication events 
     refAuth.onAuth(function(authData) { 
     $scope.auth(authData); 
     }); 
    }); 

    ... 
    }; 
    ... 

也就是说,这已经足够了继续前进的身份验证事件手表回调里面bindTo对用户火力地堡对象。

相关问题