Logout users after a period of inactivity

The script below will log out users after a period of inactivity.

<script>
function idleTimer() {
    var t;
    window.onload = resetTimer;
    window.onmousemove = resetTimer; // catches mouse movements
    window.onmousedown = resetTimer; // catches mouse movements
    window.onclick = resetTimer;     // catches mouse clicks
    window.onscroll = resetTimer;    // catches scrolling
    window.onkeypress = resetTimer;  //catches keyboard actions

    function logout() {
        deleteCookie('jwtToken');
        window.location.href = '/';
    }

   function resetTimer() {
        clearTimeout(t);
        t = setTimeout(logout, 1800000);  // time is in milliseconds (1000 is 1 second)
    }

    function deleteCookie(name) { 
        document.cookie = name + "=;path=/; expires=Thu, 01 Jan 1970 00:00:00 UTC" + ";" + 'SameSite=None; Secure'; 
    }

}
idleTimer();
</script>

based on Inactivity timeout javascript · GitHub

8 Likes

Hi Artur ! Thanks for your tips, this makes sense if we have the ability to disconnect users even if they leave the page, after a certain period of time (by the API for example). In the same principle of securing a banking site. Any idea about that?

That’s in the plans :slight_smile:

1 Like