Created
November 12, 2021 15:47
-
-
Save obeduri/b76a175eec2975a532c6db721626127d to your computer and use it in GitHub Desktop.
Session Time out with controls for multiple Tabs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SETS THE TIME FOR THE COOKIE TO EXPIRE | |
function setTimeUp() { | |
//DELETE COOKIE EXIST | |
deleteCookie('TimeIsUp'); | |
deleteCookie('TimeRedirect'); | |
deleteCookie('TimeWarn'); | |
//SETTING SESSION TIME'S UP | |
setCookie('TimeIsUp', moment().add(18, 'minutes').format('YYYY-MM-DD HH:mm:ss'), 1); | |
setCookie('TimeRedirect', moment(getCookie('TimeIsUp')).add(60, 'seconds').diff(moment()), 1); | |
setCookie('TimeWarn', getCookie('TimeRedirect') - 60000, 1); | |
} | |
function ShowWarning(Warn) { | |
if (Warn) //WILL SHOW WARNINGS | |
setCookie('Warning', 1, 1); | |
else //WONT SHOW WARNINGS | |
setCookie('Warning', 0, 1); | |
} | |
function UpdateSession() { | |
$.ajax({ | |
url: $('#hfRuta').val(), | |
data: JSON.stringify({ | |
'UserID': $('#hfUsuarioID').val() | |
}), | |
contentType: "Application/json; charset=utf-8", | |
responseType: "json", | |
method: "POST", | |
async: false, | |
success: function (response) { | |
//alert('done'); | |
}, | |
error: function (xhr) { | |
alert(xhr.status); | |
}, | |
Failure: function (response) { | |
alert(response); | |
} | |
}); | |
} | |
//ARITIFICIAL SET OF TIMESUP | |
$(document).ready(function () { | |
deleteCookie('TimeIsUp'); | |
setCookie('LoggedOut', false, 1); | |
setTimeUp(); | |
}); | |
//EVERY RUNNING PROCCES TO CHECK SESSION AND IT'S TIME | |
let swalVisible = true; | |
let debug = false, | |
loop = setInterval(() => { | |
if (getCookie('Warning') != 1) { | |
ShowWarning(true); | |
swalVisible = true; | |
$('#session-timeout-dialog').modal('hide'); | |
//if (debug) console.log('CleanedWarning'); | |
} | |
if (getCookie('LoggedOut') == "true") { | |
window.location.href = '../login.aspx'; | |
} | |
if (debug) console.log('loop'); | |
if (moment().isAfter(moment(getCookie('TimeIsUp'))) && getCookie('TimeIsUp') !== null) { | |
let firsTime = true; | |
$(document).on('click', '.go-out', function () { | |
if (debug) console.log('go out'); | |
swal.clickConfirm(); | |
}); | |
$(document).on('click', '.stay-in', function () { | |
if (debug) console.log('stay in'); | |
swal.clickCancel(); | |
}); | |
swal({ | |
type: 'warning', | |
title: "!Atención!", | |
html: `Su sesión expirará en <b><label id='lblSecondSwal'>0</label></b> segundos, seleccione <strong>Mantenerse conectado </strong> para continuar. | |
<br> | |
<div class='row'> | |
<div class='col-6'> | |
<button class='btn btn-sm btn-primary go-out'><i class='flaticon-lock-1'></i> Cerrar sesión</button> | |
</div> | |
<div class='col-6'> | |
<button class='btn btn-sm btn-info stay-in'><i class='flaticon-lock'></i> Mantener sesión</button> | |
</div> | |
</div> | |
`, | |
timer: 60000, | |
progress: true, | |
showConfirmButton: false, | |
showCancelButton: false, | |
}).then((result) => { | |
if (debug) console.log(result); | |
switch (true) { | |
case result.value == true: case result.dismiss == 'timer': | |
deleteCookie('TimeIsUp'); | |
setCookie('LoggedOut', true, 1); | |
firsTime = false; | |
window.location.href = '../login.aspx'; | |
break; | |
case result.dismiss == 'overlay': case result.dismiss == 'cancel': | |
//SCRIPT THAT RENEWS ALL THE PROCESS TO GET OVER IT AGAIN... | |
if (debug) console.log('Renews'); | |
ShowWarning(false); | |
swalVisible = true; | |
UpdateSession(); | |
firsTime = false; | |
break; | |
default: | |
} | |
}); | |
setInterval(function () { | |
var countDownDate = moment(getCookie('TimeIsUp')).add(60000, 'seconds'); | |
diff = countDownDate.diff(moment()); | |
$('#lblSecondSwal').html(moment.utc(diff).format("ss")); | |
}, 1000); | |
} else { | |
ShowWarning(true); | |
swalVisible = true; | |
$('#session-timeout-dialog').modal('hide'); | |
} | |
}, 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment