Files
repo-smod-atsc/out/rootfs/aw/anywave/web/www/js/auth.js
T

73 lines
1.9 KiB
JavaScript
Executable File

(function() {
'use strict';
var CHECK_INTERVAL = 30000;
var SESSION_CHECK_URL = 'login.cgi';
function initSessionCheck() {
if (window.location.pathname.indexOf('login.html') !== -1) {
return;
}
checkSession();
setInterval(checkSession, CHECK_INTERVAL);
document.addEventListener('mousemove', resetTimer);
document.addEventListener('keydown', resetTimer);
document.addEventListener('click', resetTimer);
}
function checkSession() {
var xhr = new XMLHttpRequest();
xhr.open('GET', SESSION_CHECK_URL + '?t=' + Date.now(), true);
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var response = xhr.responseText || '';
if (response.indexOf('UNAUTHORIZED') !== -1) {
redirectToLogin();
return;
}
if (response.indexOf('OK') !== -1) {
return;
}
}
};
try {
xhr.send();
} catch (e) {
console.log('Session check failed:', e);
}
}
function redirectToLogin() {
var currentUrl = window.location.href;
var loginUrl = 'login.html?redirect=' + encodeURIComponent(currentUrl);
if (window.top !== window.self) {
window.top.location.href = loginUrl;
} else {
window.location.href = loginUrl;
}
}
var sessionTimer;
function resetTimer() {
clearTimeout(sessionTimer);
sessionTimer = setTimeout(function() {
checkSession();
}, 60000);
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initSessionCheck);
} else {
initSessionCheck();
}
})();