Auto logout after X minutes of inactivity

Hi,
I am developing a node app. I want to implement "Auto Logout" if user is idle for a few mins say 5-10. Is there anyway wappler supports this natively?

  1. Create Server Connect (BackEnd) action that will Logout the user
  2. Add this Server Connect action to your page and name it "serverconnectLogout"
  3. Use JavaScript code bellow
const inactivityTimeout = 15; // Change to your desired X minutes
const tabInactiveTimeout = 15; // Change to your desired Y minutes

// Variables to track activity and last active time
let active = true;
let lastActiveTime = Date.now();

// Event listeners for user interaction
document.addEventListener('mousemove', () => {
active = true;
lastActiveTime = Date.now();
});

document.addEventListener('keydown', () => {
active = true;
lastActiveTime = Date.now();
});

document.addEventListener('click', () => {
active = true;
lastActiveTime = Date.now();
});

// Event listener for window visibility change
document.addEventListener('visibilitychange', () => {
if (!document.hidden) {
  active = true;
  lastActiveTime = Date.now();
}
});

// Function to check for inactivity and log message
function checkInactivity() {
const now = Date.now();
const inactiveTime = Math.floor((now - lastActiveTime) / 1000 / 60);

if (!active && (inactiveTime >= inactivityTimeout || !document.hidden && inactiveTime >= tabInactiveTimeout)) {
  console.log('User is not interacting. Initializing log out procedure.');
  dmx.parse('serverconnectLogout.load({})');
}

active = false; // Reset active flag after checking
}

// Start checking for inactivity at regular intervals
setInterval(checkInactivity, 1000 * 60); // Check every minute

inactivityTimeout stands for time out if page is in active tab of a browser.
tabInactiveTimeout stands for time out when page tab is on-focused.

If you want to implement Logout timer on a back-end - you have to create a specific column in your users database that will record last user interaction timestamp and run scheduled server connect - that will execute logout for users who got last interaction timestamp > x minutes from NOW.

3 Likes

You can also check out this thread here:

And use click event like @brad suggests:

2 Likes