I'm having a heck of a time creating drupal services from angular hitting the rest server. I created a rest end point for all my authentication needs, configured in the Service module. When I use POSTMAN plugin to send json data to the endpoints, the register, login, and logout all works correctly. On POSTMAN, I'm able to send the logout endpoint an X-CSRF-Token to force the logout and it's successful. But when I try to recreate this from my Angular app, it discontinues the workflow from login to logout. Here is the following code:
//Called from this.Login success callback, so after you log in,
//this is called to see if I can fetch the same token I logged in with,
//then I'll compare that fetched token with the one created from login
//if they are different, session is not being kept on the server
//if they are the same, drupal is aware that a session is happening
var TokenCompare = function (tokenToCompareFromLogin) {
return $http({
method: 'POST',
url: restAuthConfig.root + '/1.0/user/token',
headers: {
'Content-Type': 'application/json'
}
}).then(function(response) {
if (response.data.token == tokenToCompareFromLogin) {
$log.log ("We are finally logged in","");
} else {
$log.log ("We are not logged in", "");
}
$log.log('$membership.Token', response.data.token);
$log.log('$membership.Login', tokenToCompareFromLogin);
}, function(error) {
_isAuthenticated = false;
$log.log('$membership.IsAuthenticated - Error', error);
throw error;
});
}
this.Login = function ($userCreds) {
$log.log('$membership.Login()', "");
var _requestObject = {
"username" : $userCreds.email,
"password" : $userCreds.password
};
return $http({
method: 'POST',
url: restAuthConfig.root + '/1.0/user/login',
headers: {
'Content-Type': 'application/json'
},
data: _requestObject
}).then(function(response) {
TokenCompare(response.data.token)
var sessId = response.data.sessid;
var sessName = response.data.session_name;
$cookies.put(sessName, sessId);
$cookies.put("X-CSRF-Token",response.data.token);
$cookies.put("CSRF-Token",response.data.token);
_isAuthenticated = true;
return {
data: response.data,
status: response.status,
raw: response
};
}, function(error) {
_isAuthenticated = false;
$log.log('$membership.Login - Error', error);
throw error;
});
}
this.Logout = function () {
_isAuthenticated = false;
return $http({
method: 'POST',
url: restAuthConfig.root + '/1.0/user/logout',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'X-CSRF-Token': $cookies.get('X-CSRF-Token')
}
}).then(function(response) {
deleteCookie("X-CSRF-Token");
return {
data: response.data,
status: response.status,
raw: response
};
}, function(error) {
$log.log('$membership.Logout - Error', error);
throw error;
});
}
Then from my controller I have the following lines:
$membership.Login({ email : "[email protected]", password: "1234" });
$membership.Logout();
$membership is the angular service containing the above endpoints.
I was able to register new users, and after calling $membership.Login, I was able to receive a csrf token, and a sessid and sessname, and the response was a 200OK. But when I execute the $membership.Logout(), I get a 406 Not acceptable: User is not logged in. When I look at the TokenCompare function, the token received from api_auth/1.0/token is different than the token received from api_auth/1.0/login endpoint. So I have every reason to believe that a session is started from the login endpoint, is ends as soon as the $http finishes its functionality.
It's a bit difficult to find out that cause, as I mentioned, it works in POSTMAN.