Managing user sessions in SAP UI5 application

You can make use of setTimeout and clearTimeout functions to manage user sessions in SAP UI5 applications. In order to trace the activity of the user, you can either make use of mouse move event or key press event or even both to detect user activity and prevent unwanted session timeouts.

Session Timeout Implementation

Session management works by setting a timer that will expire after a specified period of inactivity. When user activity is detected, the timer is reset, extending the session duration. This approach ensures that active users remain logged in while inactive sessions are automatically terminated for security purposes.

Example

You can reset the timer on the occurrence of either of the two events. Here's a complete implementation ?

// Session timeout duration in milliseconds (30 minutes)
var sessionDuration = 30 * 60 * 1000;
var sessionTimer;

// Initialize session timeout
function initSessionTimeout() {
    sessionTimer = setTimeout(sessionTimeout, sessionDuration);
}

// Reset session timer on user activity
function resetSessionTimer() {
    clearTimeout(sessionTimer);
    sessionTimer = setTimeout(sessionTimeout, sessionDuration);
}

// Handle session timeout
function sessionTimeout() {
    alert("Your session has expired due to inactivity. Please log in again.");
    // Redirect to login page or perform cleanup
    window.location.href = "/login";
}

// Event listeners for user activity
document.onmousemove = resetSessionTimer;
document.onkeypress = resetSessionTimer;
document.onclick = resetSessionTimer;

// Initialize the session timer when page loads
window.onload = function() {
    initSessionTimeout();
};

In this implementation, the sessionDuration variable defines how long a session should remain active (30 minutes in this example). The resetSessionTimer function clears the existing timer and sets a new one, effectively extending the session whenever user activity is detected.

Conclusion

This session management approach provides a simple yet effective way to handle user sessions in SAP UI5 applications by automatically logging out inactive users while keeping active sessions alive based on user interaction events.

Updated on: 2026-03-13T18:26:02+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements