Skip to content

Instantly share code, notes, and snippets.

@sadewole
Created January 4, 2021 15:57
Show Gist options
  • Save sadewole/fa845fa84cf5d9fe077f5d01e2db90a3 to your computer and use it in GitHub Desktop.
Save sadewole/fa845fa84cf5d9fe077f5d01e2db90a3 to your computer and use it in GitHub Desktop.
Defines functions for auto session timeout
let timeStamp;
let warningInactiveInterval = useRef();
let startTimerInterval = useRef();
// start inactive check
let timeChecker = () => {
startTimerInterval.current = setTimeout(() => {
let storedTimeStamp = sessionStorage.getItem('lastTimeStamp');
warningInactive(storedTimeStamp);
}, 60000);
};
// warning timer
let warningInactive = (timeString) => {
clearTimeout(startTimerInterval.current);
warningInactiveInterval.current = setInterval(() => {
const maxTime = 2; // Maximum ideal time given before logout
const popTime = 1; // remaining time (notification) left to logout.
const diff = moment.duration(moment().diff(moment(timeString)));
const minPast = diff.minutes();
const leftSecond = 60 - diff.seconds();
if (minPast === popTime) {
setSecond(leftSecond);
}
if (minPast === maxTime) {
clearInterval(warningInactiveInterval.current);
sessionStorage.removeItem('lastTimeStamp');
// your logout function here
}
}, 1000);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment