Does $ resource delete function not work as expected?
I created a simple application in Angular using a simple API that I created myself using Laravel. The application is hosted here . The API is hosted here . Now I can log into the application, after which the API returns a simple auth_token
one that is sent as a URL parameter in every subsequent request that is sent to the server.
There is only one user in the system:
Email: admin@admin.com
Password: admin12345
You can log into the application using these credentials, after which the application will set a cookie using the service $cookieStore
and will use the token in this cookie for each subsequent request. After using the application, the user can exit the application, where the request is DELETE
sent to the server and by the success method, the cookie is removed from the browser.
Unfortunately, there are some problems with the code I'm guessing. The request DELETE
works as expected and it deletes auth_token
on the server and returns 200 OK
. But the method of success is not named. I'm not sure what I am doing wrong. It might just be a syntax problem.
app.js
function AppCtrl ($scope, $cookieStore, $location, Auth) {
$scope.setActive = function (type) {
$scope.destinationsActive = '';
$scope.flightsActive = '';
$scope.reservationsActive = '';
$scope[type + 'Active'] = 'active';
};
$scope.authenticate = function (credentials) {
Auth.save(credentials, function(data){
$cookieStore.put('auth_token', data.auth_token);
$scope.isLoggedIn = true;
$location.path('destinations');
$scope.message = null;
}, function(data){
$scope.message = "Email/Password combination incorrect!";
});
};
$scope.logout = function () {
//var auth_token = $cookieStore.get('auth_token');
Auth.delete({
'auth_token': $cookieStore.get('auth_token')
}, function(data){
$scope.isLoggedIn = false;
$cookieStore.remove('auth_token');
});
};
if($cookieStore.get('auth_token')){
$scope.isLoggedIn = true;
}else{
$scope.isLoggedIn = false;
}
}
The exit function is called when the exit button is pressed. What am I doing wrong here?
Note. The app doesn't work in Chrome for some reason (use Firefox). If you can shed some light on this it will be very helpful.
Both repositories are public, if you want to take a look:
AngulAir app: http://gitlab.learningtechasia.com:8901/rohan0793/angulair.git
AngulAirAPI: http://gitlab.learningtechasia.com:8901/rohan0793/angulairapi.git
source to share
Here is your solution
$scope.logout = function () {
//var auth_token = $cookieStore.get('auth_token');
Auth.delete(
{'auth_token': $cookieStore.get('auth_token')}, // parameters
{},//postData, which you don't need for this
function(data){
$scope.isLoggedIn = false;
$cookieStore.remove('auth_token');
},
// error callback
function (httpResponse) {
// do what you want for error handling here
}
);
};
Note: -> (below the point the issues are resolved)
- Only the second option (postdata) is missing from the $ resource.delete API. We have to specify it as blank {} if not required by the API.
- And the delete method must return a 204 status code in order to execute the success callback.
source to share