0
votes

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)

1
Don't you have to return a promise to use then ?jdmdevdotnet
Well I don't know. I'm struggling with AngularJs. I've read that calling the then method of a promise returns a new derived promise but i don't know which method I should use.Thomas Fournet

1 Answers

1
votes
.then(function(success) {
      $rootScope.currentUser = {
        id: **response**.user.id,
        tokenId: **response**.id,
        email: email,
        firstname: **response**.user.firstname,
        lastname: **response**.user.lastname
      };
      return true;
    },

The response variable is not defined in the "success" callback. "Success" argument variable should be used instead or it should be renamed to response probably.