I'm trying to use the login function of my AuthService factory from my AuthLoginController controller. The problem is that when the User.login function is executed with a wrong email and password, the console.log() returns false which is what I want. But when i use the right email and password I'm getting a
Error: response is not defined
I don't get it because everything works fine with function(error){ } but not with function(success){ } even though they are both the result of an asynchronous call.
angular
.module('app')
.factory('AuthService', ['Member', '$q', '$rootScope', '$state', function(
User, $q, $rootScope, $state) {
function login(email, password) {
return User
.login({email: email, password: password})
.$promise
.then(function(success) {
$rootScope.currentUser = {
id: response.user.id,
tokenId: response.id,
email: email,
firstname: response.user.firstname,
lastname: response.user.lastname
};
return true;
},
function(error) {
return false;
}
);
} return {
login: login
};
}]);
Here is my AuthLoginController controller.
angular
.module('app')
.controller('AuthLoginController', ['$scope', 'AuthService', '$state',
function($scope, AuthService, $state) {
$scope.user = {
email: '[email protected]',
password: 'test1'
};
$scope.login = function() {
AuthService.login($scope.user.email, $scope.user.password)
.then(function(response) {
console.log(response);
return;
}
//go to the default state after login
$state.go('test');
});
};
}]);
How can I retrieve true from my AuthLoginController ? Thanks (Sorry for my bad english)
then
? – jdmdevdotnet