0

I am trying to write a service to authenticate users on login:

//Authentication Service
app.factory("AuthenticationService", function($location, $http) {
  return {     
    login: function(credentials) {

    $http('api/auth/:username/:password',{'username': credentials.username, 'password' : credentials.password})
        .success(function(data,status,headers,config){
            if(data.success=="1"){
                //succefull login
                credentials.isLogged = true;
                credentials.username = data.username;
                return true;
            }
            else{
                credentials.isLogged = false;
                credentials.username = '';
                credentials.errormessage = "Login Failed! Try again...";
                return false;
            }
        })
        .error(function(data,status,headers,config){
            credentials.isLogged = false;
            credentials.username = '';        
            credentials.errormessage = "Login Error. Missing Service";
            return false;
        });

    },

    logout: function() {
      $location.path('/logout');
    },

    checkAuth: function(){
        if (credentials.isLogged) {
            return true;
        } else {
            $location.path('/admin/login');
        }
    }

  };
});

In the backend I have a PHP api that responds with:

[{'success':'0'}]

However I cannot manage to make the call appropriately from Angular, since it always return the "Login Error" message.

Can someone have a look at this code and tell me what I am doing wrong? (I guess many things). Thanks a lot

EDIT

I changed the PHP to return: {'success':'0'} (or 1, accordingly) Also I tried

$http.post(...)

But I get 404. The URL is definitively correct, I must be doing something else wrongly...

2 Answers 2

1

Try with {'success':'0'}, I mean you can't return arrays as root object.

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

Comments

0

Try referencing the index of the array where your object is contained:

if (data[0].success=="1")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.