title: AngularJS automatic log out when closing browser window
body: <p>I am writing an app using AngularJS and PASSPORT to authenticate. I want to have the functionality where when logged in, a user automatically gets logged out when the browser window closes. </p>
  
  <p>This is my log out function in my factory:</p>
  
  <pre><code>app.factory('authenticate', ['$http', '$window', function($http, $window){
     authenticate.logOut = function(){
        $window.localStorage.removeItem('token');
     };
  </code></pre>
  
  <p>and I tried to add this to a script at the bottom of the html:</p>
  
  <pre><code>window.onbeforeunload = logOut();
  </code></pre>
  
  <p>But this does not work. What should I do differently?</p>