0

I had a project using AngularJS 1.5.8 and the login method worked as followed:

$scope.login = function() {
    // creating base64 encoded String from user name and password
    var base64Credential = btoa($scope.username + ':' + $scope.password);
    // calling GET request for getting the user details
    $http.get('user', {
        headers : {
            // setting the Authorisation Header
            'Authorization' : 'Basic ' + base64Credential
        }
    }).success(function(res) {
        $scope.password = null;
        if (res.authenticated) {
            $scope.message = '';
            // setting the same header value for all request calling from
            // this application
            $http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
            AuthService.user = res;
            $rootScope.$broadcast('LoginSuccessful');
            $state.go('dashboard');
        } else {
            $scope.message = 'Login Failed!';
        }
    }).error(function(error) {
        $scope.message = 'Login Failed!';
    });
};

This was getting the info from database using spring boot with this Request

@RequestMapping("/user")
    public Principal user(Principal principal) {
        return principal;
    }

The code has to be updated to run on AngularJS 1.6.8 so I've been following tutorials I have found online etc, and now have this:

$scope.login = function() {
    // creating base64 encoded String from user name and password
    var base64Credential = btoa($scope.username + ':' + $scope.password);
    // calling GET request for getting the user details
     $http({
          url: 'user',
          method: 'GET',
          headers : {
                // setting the Authorisation Header
                'Authorization' : 'Basic ' + base64Credential
            }
        })
        .then(function onSuccess(res) {
        $scope.password = null;
        if (res.authenticated) {
            $scope.message = '';
            // setting the same header value for all request calling from
            // this application
            $http.defaults.headers.common['Authorization'] = 'Basic ' + base64Credential;
            AuthService.user = res;
            $rootScope.$broadcast('LoginSuccessful');
            $state.go('dashboard');
        } else {
            $scope.message = 'Login Failed!';
        }
    }, function onError(res) {
        $scope.message = 'Login Failed!';
    });
};

Problem is that I keep getting login failed but the users are in the database, and .success has been deprecated so no idea no idea what I've done wrong help greatly appreciated?

lin
  • 17,956
  • 4
  • 59
  • 83

1 Answers1

2

Have a look at this: Why are angular $http success/error methods deprecated? Removed from v1.6?

The success and error methods are doing some work behind the scenes to unwrap the response.data. So you may need to do

if (res.data.authenticated) ....
Kyle Lussier
  • 112
  • 1
  • 2