Reputation: 619
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
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