Reputation: 17701
We have a 'logged in' cookie which is set when signing into a clients CMS (via asp.net). Dependant whether the user has asked the site to 'remember me' or not the cookie will either have an expiry time/date set or just be a session cookie.
I need to provide an idle time message on the site via a JS script.
After 5 mins inactivity the script needs to check if the cookie is still active or not.
The inactivity check part of the script works fine - but there doesnt seem to be any way (I can find at least) to check a cookies expiry time / date / type via JS - so i'm not sure how to apprach the cookie check element. I wondered whether anywone was aware of a method to check whether the cookie is session or dated? Or Active? I noticed that the cookie itself is removed when expired on windows/ chrome - but retained despite expired on mac? so not sure a simple read check for the cookie name would be foolproof cross-device.
heres my code so far -
var idleTime = 0;
$(document).ready(function () {
var idleInterval = setInterval(timerIncrement, 60000);
$(this).mousemove(function (e) {
idleTime = 0;
});
$(this).keypress(function (e) {
idleTime = 0;
});
});
function timerIncrement() {
idleTime = idleTime + 1;
if (idleTime > 4) {
triggerAlertSortCookie();
}
}
function triggerAlertSortCookie() {
//Check if AALogin cookie has expired - show message if so & refresh page.
}
Any advice / suggested code apprach would be great. thanks
Upvotes: 1
Views: 1317
Reputation: 182
To clarify that I understand what you wanna do: you want to check if the cookie is valid at this moment.
If that's correct, the best way is to implement simple request on server-side that will return boolean response or maybe 200 ok
/ 401 unauthorized
status code.
Also, you can just return 401 unauthorized
status code for any request made with not valid cookies, and in js you can refresh, redirect to login page, etc.
Upvotes: 1