0

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.

2 Answers 2

2

window.onbeforeunload = logmeout();

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your quick response :) Seeing this answer, I feel my big problem getting solved easily. Will accept your answer once I finish implementing.
Thank you. I use this for all my logouts on browser close. It ensures that the server event of the logout still gets fired off.
More angular way would be using $window. There is no difference in functionality but saves some pain in testing.docs.angularjs.org/api/ng/service/$window
Yeah, if you are in the angular realm, its good to use $ scopes.
@judson-terrell: Now I have a problem with this. If I add window.onbeforeunload inside html page with a return message it works. but How to call an angular function with this?, If I put 'window.onbeforeunload` in the main controller, it works only when controller change and not when browser closed. So its not working. If I write logout outside the angular, I cannot get the username and session info. Even the $cookieStore I use works within angular only. How to solve this problem? Am I doing something wrong?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.