2014-11-20 188 views
1

我正在尝试使用Angularjs进行简单的登录功能。我分离了控制器并提供了一项服务,通过Firebase服务器对用户进行身份验证。我应该提到的另一件事是,我使用他们的API称为Angular Fire。问题是我试图触发$ rootScope。$ on函数。下面的代码:

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 
});// 

我甚至记录根范围内,看看我有()函数$内正确参数:

$$listeners: Object$firebaseAuth:authWithPassword: Array[1]0: function (e, authUser){length: 1 
__proto__: Array[0] 

请如果有人能帮助我将不胜感激。

+0

在使用你的身份验证服务?您是否在代码中的任何其他位置使用Firebase进行身份验证? – GregL 2014-11-21 00:07:08

+0

我在AngularFire的代码中看不到广播'$ firebaseAuth:authWithPassword'事件的地方。这是你在代码中播出的事件吗? – GregL 2014-11-21 00:54:08

+0

如果你看看认证服务,它应该在那里。 $ firebaseAuth是一个参考,所以它应该有东西 – gmetrix 2014-11-21 01:02:50

回答

6

Angular Fire不会在范围上广播事件。您需要在承诺完成回调中执行此操作。我会建议您在身份验证服务中这样做。

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所以你可以在相同的诺言链额外的动作(如你在这个RegistrationController做)。

+0

哇!这解决了我的问题。非常感谢!!! – gmetrix 2014-11-21 01:34:53

+0

谢谢!也帮助了我。 – solefald 2015-01-03 03:48:18

0

我怀疑你的StatusController在登录由RegistrationController执行时不活跃。任何机会,StatusController/meetings路线的控制器?如果是这种情况,您的控制器只是“太迟”而无法听到事件的发生。

+0

不,我有一个与认证无关的控制器。我只是用它来登录后重定向。 – gmetrix 2014-11-21 01:06:25

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; 
    } 
}); 

}); 
相关问题