0

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)

2
  • Don't you have to return a promise to use then ? Commented Feb 7, 2017 at 21:35
  • 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. Commented Feb 7, 2017 at 21:48

1 Answer 1

1
.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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm ashamed of myself i've read those lines of code so much that i became blind. This mistake is so stupid. Thanks man !

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.