title: How to expire session due to inactivity in Django?
body: <p>Our Django application has the following session management requirements.</p>
  
  <ol>
  <li>Sessions expire when the user closes the browser.</li>
  <li>Sessions expire after a period of inactivity.</li>
  <li>Detect when a session expires due to inactivity and display appropriate message to the user.</li>
  <li>Warn users of a impending session expiry a few minutes before the end of the inactivity period. Along with the warning, provide users an option to extend their session.</li>
  <li>If user is working on a long business activity within the app that doesn't involve requests being sent to the server, the session must not timeout.</li>
  </ol>
  
  <p>After reading the documentation, Django code and some blog posts related to this, I have come up with the following implementation approach.</p>
  
  <p><strong>Requirement 1</strong><br>
  This requirement is easily implemented by setting SESSION_EXPIRE_AT_BROWSER_CLOSE to True.</p>
  
  <p><strong>Requirement 2</strong><br>
  I have seen a few recommendations to use SESSION_COOKIE_AGE to set the session expiry period. But this method has the following problems.</p>
  
  <ul>
  <li><p>The session always expires at the end of the SESSION_COOKIE_AGE even if the user is actively using the application. (This can be prevented by setting the session expiry to SESSION_COOKIE_AGE on every request using a custom middleware or by saving the session on every request by setting SESSION_SAVE_EVERY_REQUEST to true. But the next problem is unavoidable due to the use of SESSION_COOKIE_AGE.)</p></li>
  <li><p>Due to the way cookies work, SESSION_EXPIRE_AT_BROWSER_CLOSE and SESSION_COOKIE_AGE are mutually exclusive i.e. the cookie either expires on browser close or at the specified expiry time. If SESSION_COOKIE_AGE is used and the user closes the browser before the cookie expires, the cookie is retained and reopening the browser will allow the user (or anyone else) into the system without being re-authenticated.</p></li>
  <li><p>Django relies only on the cookie being present to determine if the session is active. It doesn't check the session expiry date stored with the session.</p></li>
  </ul>
  
  <p>The following method could be used to implemented this requirement and to workaround the problems mentioned above.</p>
  
  <ul>
  <li>Do not set SESSION_COOKIE_AGE.</li>
  <li>Set the expiry date of the session to be 'current time + inactivity period' on every request.</li>
  <li>Override process_request in SessionMiddleware and check for session expiry. Discard the session if it has expired.</li>
  </ul>
  
  <p><strong>Requirement 3</strong><br>
  When we detect that the session has expired (in the custom SessionMiddleware above), set an attribute on the request to indicate session expiry. This attribute can be used to display an appropriate message to the user.</p>
  
  <p><strong>Requirement 4</strong><br>
  Use JavaScript to detect user inactivity, provide the warning and also an option to extend the session. If the user wishes to extend, send a keep alive pulse to the server to extend the session.</p>
  
  <p><strong>Requirement 5</strong><br>
  Use JavaScript to detect user activity (during the long business operation) and send keep alive pulses to the server to prevent session from expiring.</p>
  
  <hr>
  
  <p>The above implementation approach seem very elaborate and I was wondering if there might a simpler method (especially for Requirement 2). </p>
  
  <p>Any insights will be highly appreciated.</p>