Rohan
Rohan

Reputation: 13781

$resource delete function not working as expected?

I have built a simple application in Angular consuming a simple API I created myself using Laravel. The application is hosted here. The API is hosted here. Now I can log in to the application at which point the API returns a simple auth_token which is sent as the URL parameter in every subsequent request that is sent to the server.

There is only one user in the system:

Email: [email protected]
Password: admin12345

You can log into the application using these credentials at which point the application will set a cookie using the $cookieStore service and will use the token in this cookie for every subsequent request. After using the application, a user can log out from the application, where a DELETE request is sent to the server and on the success method, the cookie is deleted from the browser.

Unfortunately there is some issue with the code I suppose. The DELETE request is working as expected and it deletes the auth_token on the server and returns 200 OK. But the success method is not called. I am not sure what I am doing wrong. It might be just 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 logout function is called when the log out button is pressed. What am I doing wrong here?

Note: The application is not working on Chrome for some reason (Use Firefox). If you can shed some light on that, it would be very helpful.

Both the repositories are public if you wish to have a look:

AngulAir Application: http://gitlab.learningtechasia.com:8901/rohan0793/angulair.git

AngulAirAPI: http://gitlab.learningtechasia.com:8901/rohan0793/angulairapi.git

Upvotes: 3

Views: 509

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

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 points solved the problem)

  1. Only the 2nd option(postdata) in $resource.delete API was missing. We should give it as a blank {} if it is not required for API.
  2. And delete method should return 204 Status Code in order to execute success callback.

Upvotes: 2

Related Questions