If you have a web page which updates itself using Ajax background refresh tasks, when the user's session is timed-out, the response of the refreshing Ajax request will be a
302 redirect to log-in page. But the Ajax request may not be able to handle that and simply fails. The user may not see the updated results. In this case, web page should detect the session timeout and redirect the user to login page.
When using AngularJS's
$http service for Ajax request, it's very simple to handle session timeout. All you need to do is to add a
$http interceptor and handle the response. See CoffeeScript code below.
angular.module('myModule', ) .factory('sessionTimeoutInterceptor', [() -> response: (response) -> if angular.isString(response.data) && response.data.indexOf('Log in') != -1 window.location.reload() response ]) .config(['$httpProvider', ($httpProvider) -> $httpProvider.interceptors.push('sessionTimeoutInterceptor') ])
In the code above, an interceptor
sessionTimeoutInterceptor is added. This interceptor will check Ajax response. If response is a string and it contains certain text, e.g.
Log in, then this means the session has timed-out. In this case, just refresh the page. The user will be redirected to log in page.