I see two solutions:
(1) The mechanism of "heartbeat". On each page there is a script that will "ping" the server with some fictitious ajax request, for example:
<script> setInterval(ping, 60000); </script>
Therefore, the session should not expire while the browser window is open.
(2) In each ajax request, check the status of the response. If the response contains a “401 unauthorized” code (or any other code other than 200), this means that the session has expired, and instead of loading the response into a dialog box on the page, redirects the user to the login page.
Conclusion based on comments:
The best solution would be to combine the two above mechanisms. The Heartbeat mechanism will help to keep the session as long as the page is displayed in the browser. But this does not guarantee it for sure. When the session ends, the connection to the server may be broken and resumed. So you should check the status of the response anyway.
Panjanek
source share