Skip to content

Instantly share code, notes, and snippets.

@bjpeterdelacruz bjpeterdelacruz/logout.js
Last active May 23, 2018

Embed
What would you like to do?
How to warn user that their session will timeout soon and how to log them out once it expires
var logoutUrl = "https://test-swhv.ehawaii.gov/swhv-wikiflow/logout.php";
var refreshUrl = "https://test-swhv.ehawaii.gov/swhv-wikiflow/refresh.html";
function displayTimeOutWarningMessage() {
var thirtyMinutes = 1000 * 60 * 30;
var fifteenMinutes = thirtyMinutes / 2;
var lastActivity = new Date().getTime();
var checkTimeOut = function() {
var currentTime = new Date().getTime();
if (currentTime >= lastActivity + fifteenMinutes) {
var time = moment().format("MM/DD/YYYY hh:mm A");
var warningMessage = "[" + time + "] You have been inactive for more than 15 minutes, and your session will time out in less than 15 minutes. ";
warningMessage += "Click OK to continue using this web application, or click Cancel to logout.";
if (confirm(warningMessage)) {
if (new Date().getTime() >= lastActivity + thirtyMinutes) {
alert("Your session has expired. You will now be logged out.");
window.location.href = logoutUrl;
} else {
$.ajax({
url: refreshUrl,
type: "GET"
});
lastActivity = new Date().getTime();
window.setTimeout(checkTimeOut, 1000);
}
} else {
window.location.href = logoutUrl;
}
} else {
window.setTimeout(checkTimeOut, 1000); // check once per second
}
}
checkTimeOut();
}
$(document).ready(function() {
displayTimeOutWarningMessage();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.