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()
};
});
嘿MarcoS,我發現你有類似的問題,我的。請你關心看看我的問題:http://stackoverflow.com/questions/31872935/firebase-authwithoauthredirect-doesnt-call-onauth-without-page-refresh – Nikola 2015-08-07 11:33:15
我做到了,但你是一個完全不同的(Ionic/Angular + Firebase)......對不起,我幾個月沒有使用Angular(只是等待2.0 ... :-),所以我真的不能幫你...: - ( – MarcoS 2015-08-07 14:31:36
感謝您的回覆。我設法解決了這個問題。 – Nikola 2015-08-07 16:46:01