Time out and and redirect users to log-out

Background

In order to enhance security of the site, you may want to automatically log-out your users after x minutes of inactivity on the site. This can be achieved by adding redirect code in.

If a user is logged in in multiple tabs, this script will keep all tabs logged in until no movement is detected in for 10 minutes in any of them, at which point they will all logout. 

How to

Paste the following code in:

<script>
    (function(){
        if (typeof(Storage) !== "undefined") {

            // Browser supports local storage
            // Change this variable to modify the inactive time before logout
            var minutesInactiveToLogout = 10;
            var secondsCheckLogoutFrequency = 30;
            var doReset = false;
            
            $(document).on('ready mousemove keypress', function(){ doReset = true;});

            //Check if we should log out every x frequency
            setInterval(function(){checkForLogoutCondition();}, secondsCheckLogoutFrequency * 1000);

            function checkForLogoutCondition(){
                if (doReset) {
                    resetTimer();
                    doReset = false;
                    return;
                }
                var lastActiveTime = readLocalStorage('logOutTimer');
                if(lastActiveTime){
                    currentDate = new Date();
                    currentTime = currentDate.getTime();
                    if((currentTime - (minutesInactiveToLogout * 60 * 1000)) >= lastActiveTime){
                        //Log out
                        logOut();
                    }
                }
            }

            function resetTimer(){
                var currentDate = new Date();
                var currentTime = currentDate.getTime();
                //Overwrite the localstorage
                createLocalStorage('logOutTimer',currentTime);
            }

            function logOut(){
                var currentLocation = window.location.pathname;
                location = '/logout?page='+currentLocation;
            }

            function createLocalStorage(name,value) {
                localStorage.setItem(name, value);
            }

            function readLocalStorage(name) {
                return localStorage[name];
            }
        } else {
            //No Web Storage support..
        }
    }());
</script>

Claromentis 8.7+

Navigate to 

Admin > Script

Paste the code inside head tag

Prior Claromentis 8.7

Navigate to

Admin > Design > Advanced

and paste example code below to redirect user back to the login page after 10 minutes of inactivity.

 

Created on 10 August 2017 by Michael Christian. Last modified on 20 June 2019

Was this helpful?  

3 Likes
Share