我正在尝试使用Angularjs进行简单的登录功能。我已经分离了控制器并提供了一项服务,通过Firebase服务器对用户进行身份验证。我应该提到的另一件事是我正在使用他们的名为Angular Fire的API。问题是我试图在函数上触发$ rootScope。$。这是代码:
myApp.controller('StatusController', function($scope, $rootScope, $firebase, $firebaseAuth) {
console.log($rootScope);
$rootScope.$on('$firebaseAuth:authWithPassword', function(e, authUser){
console.log("printsomething");
$scope.userEmail = authUser.userEmail;
}); //$firebaseAuth:login
});//StatusController
这是身份验证服务:
myApp.factory('Authentication', function ($firebase, $firebaseAuth, FIREBASE_URL, $location) {
var ref = new Firebase(FIREBASE_URL);
console.log(FIREBASE_URL);
var simpleLogin = $firebaseAuth(ref);
var myObject = {
login: function (user) {
return **simpleLogin.$authWithPassword**({
email: user.email,
password: user.password
});
} //login
} //myObject
return myObject;
})
以下是调用身份验证的位置。在我的注册控制器中:
myApp.controller('RegistrationController', function($scope, $firebaseAuth, $location, Authentication) {
$scope.login = function() {
Authentication.login($scope.user)
.then(function(user) {
$location.path('/meetings');
}, function(error) {
$scope.message = error.toString();
});
}//login
$scope.register = function() {
$location.path('/register');
}//register
});//
我甚至记录了根范围,看看我在$ on()函数中是否有正确的参数:
$$listeners: Object$firebaseAuth:authWithPassword: Array[1]0: function (e, authUser){length: 1
__proto__: Array[0]
如果有人可以提供帮助,我会很感激。
答案 0 :(得分:6)
Angular Fire不会在范围内广播事件。您需要在promise完成回调中执行此操作。我建议您在身份验证服务中这样做。
myApp.factory('Authentication', function ($rootScope, $firebase, $firebaseAuth, FIREBASE_URL, $location) {
var ref = new Firebase(FIREBASE_URL);
console.log(FIREBASE_URL);
var simpleLogin = $firebaseAuth(ref);
var myObject = {
login: function (user) {
return simpleLogin.$authWithPassword({
email: user.email,
password: user.password
}).then(function(authData){
$rootScope.$broadcast('$firebaseAuth:authWithPassword',authData);
return authData;
});
} //login
} //myObject
return myObject;
})
您在处理程序的末尾返回authData,这样您就可以将其他操作链接到相同的promise(就像在RegistrationController中一样)。
答案 1 :(得分:4)
我认为你可能会像我一样看一个稍微过时的教程。我稍微挖掘了Firebase文档,并为新的身份验证和状态代码提出了这些文档。基本上他们有一个$ onAuth监听器,你现在可以使用它。
验证
myApp.factory('Authentication',function($firebase,$firebaseAuth,$location,FIREBASE_URL, $rootScope){
var ref = new Firebase(FIREBASE_URL);
var simpleLogin = $firebaseAuth(ref);
var myObject = {
login : function(user){
return simpleLogin.$authWithPassword({
email: user.email,
password: user.password
})
},//login
logout: function(){
console.log("calling unauth");
return simpleLogin.$unauth();
}
}//myObject
return myObject;
});
状态:
myApp.controller('StatusController', function($scope, $rootScope,$firebaseAuth, FIREBASE_URL, $location, Authentication){
var ref = new Firebase(FIREBASE_URL);
var authObj = $firebaseAuth(ref);
$scope.logout = function(){
Authentication.logout();
$location.path('/login');
}
authObj.$onAuth(function(authData) {
if (authData) {
console.log("Logged in as:", authData.password.email);
$scope.userEmail = authData.password.email;
} else {
console.log("Logged out");
$scope.userEmail = null;
}
});
});
答案 2 :(得分:0)
我怀疑StatusController
执行登录时您的RegistrationController
无效。任何机会,StatusController
路由的控制器是/meetings
吗?如果是这样的话,你的控制器就太晚了#34;听到发出的事件。