Handling inactive users

Hi community,

We are looking for a way to handle inactivity in our apps. What we want to achieve is to force logout for a user that has been inactive for more than X seconds. Any idea on how to achieve this?

Reading through the docs and different options on App or Environment level, I can’t seem to find the functionality that can handle this. Am I missing something?

Hi @Kaspar,

I have not done this before but I imagine you can come close to something by using:
1 A timestamp on last time action was done.
2 A timed App action that runs every 5 min that checks if action done timestamp is further away than the limit of inactivity. If true run action node “log out”. https://docs.appfarm.io/reference/apps/app-settings#timers
3 Define some arbitrary way of tracking activity. For instance on view load (on every view) update the last action done timestamp.

Point 3 is probably where this is not very elegant but maybe you can figure out a better way. If so I am interested in how :slight_smile:

1 Like

Thanks @Sondre

As far as I’ve seen, your suggestions are what will get us closest to the our goal. I briefly tested implementing a timer with some timestamp, but we’re still facing the issue. The app pauses when not in focus, so the timers don’t run, and we can’t achieve a true inactive logout.

Maybe if Services had a logout action node, it could work. Or an addition to the environment session setting :thinking:

Appreciate your input!

1 Like

Hi @Sondre and @Kaspar ,

By today, there is no built-in functionality in Create to achieve this. However, you can use a Coded Component and check the users inactivity this way. I have added the logout action under “Actions” on the Coded Component. It is also possible to add other DOM events to check the inactivity.

// Set the inactivity timeout value in milliseconds (e.g., 15 minutes)
const inactivityTimeout = 15 * 60 * 1000; // 1 minute

let logoutTimer;

function resetLogoutTimer() {
  // Clear existing timer
  clearTimeout(logoutTimer);

  // Set a new timer
  logoutTimer = setTimeout(logoutUser, inactivityTimeout);
}

function logoutUser() {
  // Perform logout actions 
  appfarm.actions.logout()
    .then(() => console.log('User logged out due to inactivity!'))
    .catch(console.error)
}

// Event listeners to track user activity
function handleUserActivity() {
  resetLogoutTimer();
}

document.addEventListener("mousemove", handleUserActivity);
document.addEventListener("keydown", handleUserActivity);

// Initial setup
resetLogoutTimer();
2 Likes

Hi @Simona,

Just two questions about this method:
Does this need to be on every view?
Does this override the App pause Kaspar talks about?

Thank you :slight_smile:

Thanks @Simona - exactly what we wanted to achieve :smile:

As far as I’ve tested, you would only need this one place in the app but the app needs to visit the view in order for the coded component and script to execute. After it has been executed it will work regardless of your current view.

I implemented this in two different apps. The first are built with a view container where all views are sub views. For this type of app, I only needed the coded component on the main view.
The second app are built with multiple views. I could implement the coded component on each view but chose to add a “blank view” that the app navigates to on loaded, effectively executing the coded component and script. It then works regardless of future navigations. This might increase the load time but works until we discover a “smarter” way of achieving this :smiley:

And yes, this “overrides” the App pause (sending the app to background) which were the downside with the other strategies.

2 Likes