Issue
I'm managing an AngularJS website behind a reverse proxy used for authentication. After a few hours, that session on the reverse proxy expires.
If the website is still open in a browser, and a user click on a page, the framework will return javascript which contains not javascript, but the HTML of the reverse proxy login page, starting with <!DOCTYPE html>
This can't be parsed and provoke an error "Uncaught SyntaxError: expected expression, got '<'"
The whole page is black because of these errors.
A workaround then is to refresh the page, but all of our users don't know it.
What can I do to fix the problem gracefully ?
Solution
Use an AngularJS HTTP interceptor to check if API responses contain HTML. If it does, it means the session has expired, so redirect the user to the login page or show a session expired notification.
Here's a classic example, but you'll need to adapt it to your code:
app.factory('myInterceptor', function($q, $window) {
return {
response: function(response) {
if (typeof response.data === 'string' && response.data.includes('<!DOCTYPE html')) {
$window.location.href = '/login';
}
return response;
}
};
});
app.config(function($httpProvider) {
$httpProvider.interceptors.push('myInterceptor');
});
Hope my answer helps you
Answered By - Iliasse
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.