Pass authentication error from factory to controller in AngularJS -
i trying implement firebase authentication via factory , pass error , authdata object controller factory. possible? can't seem figure out how. keep getting undefined variables.
if print error upon failed login console.log should expected, rest of code works. can't pass error/obj controller.
my controller:
myapp.controller('logincontroller', ['$scope', '$location', 'authentication', function($scope, $location, authentication) { $scope.login = function() { authentication.login($scope.user); // error auth factory } }]);
my factory:
myapp.factory('authentication', ['$firebase', 'firebase_url', '$location', function($firebase, firebase_url, $location){ var ref = new firebase(firebase_url); var authobj = { login: function(user) { return ref.authwithpassword({ email : user.email, password : user.password }, function(error, authdata) { if (error) { // pass error controller // i've tried following: // authobj.err = error; // return authobj.err = error; } else { // pass authdata controller } }); } // login }; return authobj; }]);
you pass error handler function factory controller. (untested):
//controller myapp.controller('logincontroller', ['$scope', '$location', 'authentication', function($scope, $location, authentication) { $scope.login = function() { authentication.login($scope.user, function(error, authdata) { // access error }); } }]); //factory myapp.factory('authentication', ['$firebase', 'firebase_url', '$location', function($firebase, firebase_url, $location){ var ref = new firebase(firebase_url); var authobj = { login: function(user, errorhandler) { return ref.authwithpassword({ email : user.email, password : user.password }, errorhandler); } // login }; return authobj; }]);
Comments
Post a Comment