2
votes

I'm trying to use loopback angular SDK to login but instead I'm getting back a 401 unauthorized response.

User.login({ rememberMe: true }, {email: $scope.user.email, password: $scope.user.password})
      .$promise
      .then(function() {
          var next = $location.nextAfterLogin || '/';
          $location.nextAfterLogin = null;
          $location.path(next);
      })
      .catch(function(x) {
          $scope.authError = 'Wrong Credentials';
      });
  };

Also i can see the user with the given credentials in the datasource that I've defined for the User model.

So basically, I have a boot script that creates a default user

 User.create([{
    username: 'admin',
    email: '[email protected]',
    password: 'admin'
}], on_usersCreated);

And I also have created a custom User model that extends the built-in User model.

Why am I getting a 401 unauthorized response when trying to login? I have even tried to login via the explorer, but with no success.

Update:

I debugged the build-in User model and the login method; and it seems that it cannot find the user with the given email. I can see it though in the mongodb database.

2

2 Answers

1
votes

Inspect the ACL records of User model, starting you app in debug mode and viewing console: DEBUG=loopback:security:acl slc run.

0
votes

I don't see anything wrong with your code. Are you sure the values you're passing into the function are correct? Try logging $scope.user.email and $scope.user.password before calling User.login.

See my new example here: https://github.com/strongloop/loopback-getting-started-intermediate/blob/master/client/js/services/auth.js#L6-L15

The tutorial part is not complete, you can look around the source code to get an idea of what you're doing different.