Manoj G
Manoj G

Reputation: 1806

AngularJS: Trigger logout automatically when browser closed

I am trying to implement login Authentication module in AngularJS. Now I'm left with implementing logout.

Logout:

I'm updating login, logout and session info in SessionHistory table mainly not to allow 2 different session for a single user at a particular time.

So, function for updating logout info should be triggered for the following,

  1. When user logs out from the app.
  2. When user is idle (Session timeout).
  3. When browser is closed.

First point I did, second point, I'll manage to do (Finding many articles and questions in stackoverflow as well).

But I'm not sure how to trigger the logout function to update in database automatically, when browser is closed. I did not managed to get any article. Please help me with this. I'm also new to AngularJS

Thanks in advance.

Upvotes: 0

Views: 5587

Answers (2)

Shaze
Shaze

Reputation: 972

You will need to detect the browser/tab close which is - $window.onbeforeunload function,then delete the (localstorage) for service, register $window.onbeforeunload in the angular.run, below an example:

angular.module('app', [])
  .controller('exitController', function($scope, $window) {
    $scope.onExit = function() {
     $localStorage.$reset();//it will delete all the data from localstorage.
    };

   $window.onbeforeunload =  $scope.onExit;
  });

Please note: This will log out even if multiple tabs are opened.

Upvotes: 0

Judson Terrell
Judson Terrell

Reputation: 4306

window.onbeforeunload = logmeout();

Upvotes: 2

Related Questions