plat_winz
plat_winz

Reputation: 135

Session TImeout after logon?

Below is a logon code I have set up with html. I would like to set a timeout setting to initiate a log off function after x minutes idle time. Is this possible? I currently have a log off button that initiates the log off, so possibly have that timeout select that function. Thank you.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
        <fieldset>
            <legend>Enter credentials</legend>
            <p>
                <label for="username">User name:</label>
                <input type="text" id="username" name="username" />
            </p>
            <p>
                <label for="password">Password:</label>
                <input type="password" id="password" name="password" />
            </p>
        </fieldset>
        <input type="submit" id="login-button" name="login-button" value="Log On" />
    </form>
<script src="scripts/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

$(document).ready(function () {
      
  // Web Proxy request to fetch the configuration
  ajaxWrapper({ url: 'Home/Configuration', dataType: 'xml', success: configSuccess });

  $('form').submit(function () {
    var username = $('#username').val(),
    password = $('#password').val();

    clearMessage();

    if (!username || !password) {
      showMessage('Enter a username and a password');
      return false;
    }

    // Ensure the user name is correct...
    // If the username has the domain string at position 0, then
    // the username is correct and just use it as normal, but if
    // not, username needs to have the domain prepended.
    // Because of the backslashes in the strings, they need to be 
    // escaped with "\\"
    username = username.indexOf("domain\\") === 0 ? username : "domain\\" + username;

    // Web Proxy request to log the user on
    ajaxWrapper({
      url: 'PostCredentialsAuth/Login',
      dataType: 'xml',
      success: loginSuccess,
      error: loginError,
      data: { username: username, password: password }
    });

    return false;
  });

});

</script>

Below is the code I added to the section after the logonsuccess form is selected.

    function loginSuccess(data) {
        var $loginXml = $(data),
            result = $loginXml.find('Result').text();

        if (result == 'success') {
            $('form').hide();
            $('#log-off').show();
			
		// Set timeout variables.
var timoutWarning = 60000; // Display warning in 14 Mins.
var timoutNow = 30000; // Warning has been shown, give the user 1 minute to interact
var logoutUrl = '($configXml.find('authManager').attr('logoffURL'));'; // URL to logout page.

var warningTimer;
var timeoutTimer;

// Start warning timer.
function StartWarningTimer() {
    warningTimer = setTimeout("IdleWarning()", timoutWarning);
}

// Reset timers.
function ResetTimeOutTimer() {
    clearTimeout(timeoutTimer);
    StartWarningTimer();
    $("#timeout").dialog('close');
}

// Show idle timeout warning dialog.
function IdleWarning() {
    clearTimeout(warningTimer);
    timeoutTimer = setTimeout("IdleTimeout()", timoutNow);
    $("#timeout").dialog({
        modal: true
    });
    // Add code in the #timeout element to call ResetTimeOutTimer() if
    // the "Stay Logged In" button is clicked
}

// Logout the user.
function IdleTimeout() {
    window.location = logoutUrl;
}

And below is the Log Off button that is shown where a user can manually log off.

        $('#log-off').click(function () {
            // Web Proxy request to log the user off
            url = ($configXml.find('authManager').attr('logoffURL'));
            ajaxWrapper({ url: url, dataType: 'text', success: logoffSuccess });
            return false;
        });
    });

Upvotes: 1

Views: 1349

Answers (2)

Phani Kumar M
Phani Kumar M

Reputation: 4590

Your code doesn't call incativityTime function. Call the function on window onload (or jquery onready) like window.onload = function() { inactivityTime() };

<html>
<body>
    <script>
        window.onload = function() { inactivityTime() };

        var inactivityTime = function () 
        {
            var t;
            window.onload = resetTimer;
            // DOM Events
            document.onmousemove = resetTimer;
            document.onkeypress = resetTimer;

            function logout() {
            alert("You are now logged out.")
            //location.href = 'logout.php'
            }   

            function resetTimer() {
                clearTimeout(t);
                t = setTimeout(logout, 3000)
                // 1000 milisec = 1 sec
            }
        }
    </script>
</body>
</html>

Upvotes: 1

plat_winz
plat_winz

Reputation: 135

I added it with no success. Below is how the logon success code looks.

    function loginSuccess(data) {
    var $loginXml = $(data),
        result = $loginXml.find('Result').text();

    if (result == 'success') {
        $('form').hide();
        $('#log-off').show();

        // Web Proxy request to enumerate the resources available to the user
        url = $configXml.find('resourcesProxy').attr('listURL');
        ajaxWrapper({ url: url, success: listResourcesSuccess });
    } else {
        showMessage('Login failed - try again');
    }

    var inactivityTime = function () {
    var t;
    window.onload = resetTimer;
    // DOM Events
    document.onmousemove = resetTimer;
    document.onkeypress = resetTimer;

    function logout() {
    alert("You are now logged out.")
    //location.href = 'logout.php'
    }   

    function resetTimer() {
        clearTimeout(t);
        t = setTimeout(logout, 3000)
        // 1000 milisec = 1 sec
    }
};
}

Upvotes: 0

Related Questions