This commit improves session management within the superadmin interface by introducing a new session validation function that ensures active sessions are maintained and provides JSON responses for session expiration. Additionally, the session timeout duration is standardized, and user feedback is enhanced with alerts for expired sessions. The codebase is streamlined by removing deprecated session handling logic, improving overall clarity and maintainability. These changes aim to enhance user experience and ensure robust session handling across the application.
89 lines
2.5 KiB
JavaScript
89 lines
2.5 KiB
JavaScript
/**
|
|
* MSIN-CON-259 — Surveillance session superadmin : redirection silencieuse vers login.
|
|
*/
|
|
(function ($, config) {
|
|
if (!config || !config.active) {
|
|
return;
|
|
}
|
|
|
|
var strPingUrl = config.pingUrl;
|
|
var strLoginUrl = config.loginUrl;
|
|
var intInactive = parseInt(config.inactiveSeconds, 10) || 10800;
|
|
var intPingInterval = parseInt(config.pingInterval, 10) || 60000;
|
|
var blnRedirecting = false;
|
|
var intExpiresAt = Date.now() + (intInactive * 1000);
|
|
|
|
function escLoginUrl(strSuffix) {
|
|
if (strLoginUrl.indexOf('?') >= 0) {
|
|
return strLoginUrl + '&' + strSuffix;
|
|
}
|
|
return strLoginUrl + '?' + strSuffix;
|
|
}
|
|
|
|
window.fxSuperadmSessionExpiredRedirect = function () {
|
|
if (blnRedirecting) {
|
|
return;
|
|
}
|
|
blnRedirecting = true;
|
|
window.location.href = escLoginUrl('expired=1');
|
|
};
|
|
|
|
function resetCountdown(intExpiresIn) {
|
|
var intSeconds = parseInt(intExpiresIn, 10);
|
|
if (isNaN(intSeconds) || intSeconds <= 0) {
|
|
intSeconds = intInactive;
|
|
}
|
|
intExpiresAt = Date.now() + (intSeconds * 1000);
|
|
}
|
|
|
|
function checkLocalCountdown() {
|
|
if (blnRedirecting) {
|
|
return;
|
|
}
|
|
|
|
if (intExpiresAt - Date.now() <= 0) {
|
|
fxSuperadmSessionExpiredRedirect();
|
|
}
|
|
}
|
|
|
|
function pingSession() {
|
|
if (blnRedirecting) {
|
|
return;
|
|
}
|
|
|
|
$.getJSON(strPingUrl, function (res) {
|
|
if (!res || !res.ok) {
|
|
fxSuperadmSessionExpiredRedirect();
|
|
return;
|
|
}
|
|
|
|
resetCountdown(res.expires_in);
|
|
});
|
|
}
|
|
|
|
$(document).ajaxComplete(function (event, xhr) {
|
|
if (blnRedirecting || !xhr || !xhr.responseText) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
var res = JSON.parse(xhr.responseText);
|
|
if (!res) {
|
|
return;
|
|
}
|
|
if (res.code === 'session'
|
|
|| (res.state === 'error' && res.message && String(res.message).indexOf('Session expir') === 0)
|
|
|| (res.ok === false && res.code === 'session')) {
|
|
fxSuperadmSessionExpiredRedirect();
|
|
}
|
|
} catch (e) {
|
|
// réponse non JSON
|
|
}
|
|
});
|
|
|
|
pingSession();
|
|
setInterval(pingSession, intPingInterval);
|
|
setInterval(checkLocalCountdown, 15000);
|
|
setTimeout(checkLocalCountdown, 5000);
|
|
})(jQuery, window.Ms1SuperadmSession || null);
|