Auto Log Out of Portal Users

The first script used to log out my portal users. Now it does not. I tried variations of the second script with no luck. I originally had it in a custom code block but then moved it to the custom header section. Suggestions?

// previous working code
<script>
    let logoutTimer;
    function resetLogoutTimer() {
        clearTimeout(logoutTimer);
        logoutTimer = setTimeout(logoutUser, 3600000);  // 3600000 milliseconds = 1 hour
    }

    function logoutUser() {
        document.cookie = "jwtToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
        // Refresh the page
        window.location.reload();
    }

    // Reset the logout timer on any relevant event
    window.addEventListener('mousemove', resetLogoutTimer);
    window.addEventListener('keydown', resetLogoutTimer);
    window.addEventListener('scroll', resetLogoutTimer);

    // Initialize the logout timer when the page loads
    window.addEventListener('load', resetLogoutTimer);
</script>
// new attempt
<script type="text/javascript">
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() {
        // document.cookie = "jwtToken=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
        // localStorage.removeItem('jwtToken');
        // sessionStorage.removeItem('jwtToken');
        
        window.location.href = '/';
        // window.location.reload();
    }

    function resetTimer() {
        clearTimeout(t);
        t = setTimeout(logout, 3600000);  // time is in milliseconds (1000 is 1 second)
    }
}
idleTimer();
</script>

Hi @n8atkinson - did you manage to resolve this and logout your users?

I’m trying to do something similar and the document.cookie instruction seems to prevent the page from loading (even when using a simple test document.cookie = “testCookie=testValue”;).

Hi. I was able to get the auto log-out to work, but not the auto-redirect back to the home page.