0

In my controller, I call a service that logs the user in.

I want the service to report back to my controller whether the login has been successful or not. I tried putting return true in my success(..) function, but that doesn't seem to actually be returning anything.

My service:

loginRsrc.factory('loginSrvc', ['$http', function($http){

    return {
        http: function(){
            $http({
                method: 'POST',
                url: "http://myproj.herokuapp.com/api/signIn",
                data: $.param({email: "joe.gino@gmail.com", password: "1234567"}),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
            .success(function(res){
                console.log("res:");
                console.log(res);
                return true;
            })
            .error(function(res){
                console.log("res");
                console.log(res);
            }); 
        }
    };

My controller

loginApp.controller('loginCtrl', ['$scope', '$state', 'loginSrvc', function($scope, $state, loginSrvc){

    $scope.loginForm = {};
    $scope.loginForm.email = "";
    $scope.loginForm.password = "";

    $scope.submit = function(){
        loginSrvc.http();

    // true? Then send user to other page
CodyBugstein
  • 21,984
  • 61
  • 207
  • 363
  • I don't think it is duplicate - the linked answer is 2 years old and not specific to Angular nor to `$http`. The answers given there are long and hard to apply, whereas the question here can be given short simple answer. – Dmitri Zaitsev Apr 27 '15 at 14:24

1 Answers1

1

Service:

loginRsrc.factory('loginSrvc', ['$http', function($http){

return {
    http: function(){
        return $http({
            method: 'POST',
            url: "http://myproj.herokuapp.com/api/signIn",
            data: $.param({email: "joe.gino@gmail.com", password: "1234567"}),
            headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })

    }
};

Controller

loginApp.controller('loginCtrl', ['$scope', '$state', 'loginSrvc', function($scope, $state, loginSrvc){

$scope.loginForm = {};
$scope.loginForm.email = "";
$scope.loginForm.password = "";

$scope.submit = loginSrvc.http().then(function(response){
                  // your data will be available in "response.data"
                  //code for success/resolved promise. 
                },function(error){
                  // error object available in error variable
                  // your code for error/rejected promise.
               });
harshes53
  • 419
  • 1
  • 6
  • 17
  • That's what I ended up doing. I was originally hoping to keep the success and error logic in the resource though. – CodyBugstein Dec 10 '14 at 21:21
  • if you wanted to keep the logic for success/error in resource, how would you determine in your controller that it was a success or an error POST call? I'm not denying it can't be done but why get complicated? keep it simple buddy.. – harshes53 Dec 11 '14 at 05:02