Reputation: 2625
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...
Upvotes: 0
Views: 91
Reputation: 104795
Try referencing the index of the array where your object is contained:
if (data[0].success=="1")
Upvotes: 0
Reputation: 639
Try with {'success':'0'}, I mean you can't return arrays as root object.
Upvotes: 1