Petr Osipov
Petr Osipov

Reputation: 619

AngularJS Logout on close or refresh - REST services calls get cancelled

I have a problem here. I need to implement a logout on close or refresh, meaning when the customer navigates away from the web page, or closes browser/tab. I created a directive which is added to my forms, and which should call logout functionality (which includes the REST-call to our backend, which would trigger the Siteminder logout). The REST call is sometimes performed, but mainly gets cancelled. The console.log message nearly never appears.

.directive('gcdmAutomaticLogoutOnClose', function($rootScope, 
             $window, MyLoginService){
return {
  restrict: 'A',
  scope: true,
  controller: function($element){
    $window.onbeforeunload = function (){
      return MyLoginService.logout().then(function(){
        console.log("Logout complete");
        return null;
      });

    }}

  }

});

Any idea?

Upvotes: 1

Views: 608

Answers (1)

radius
radius

Reputation: 159

Your logout service should run synchronously (not return a promise) if you want to guarantee it will execute before the page unloads.

Angular's $http service runs async by design, but here's a link showing how to make a synchronous service:

Upvotes: 1

Related Questions