2014-10-17 31 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對用戶火力地堡對象。

相關問題