Created
January 4, 2021 15:57
-
-
Save sadewole/fa845fa84cf5d9fe077f5d01e2db90a3 to your computer and use it in GitHub Desktop.
Defines functions for auto session timeout
This file contains 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
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