Tips & Tricks

Setting up a logout after a specific duration

App Builder & Automation Expert

Stay Updated with ProcFu!

Subscribe to our newsletter for the latest tips, updates, and automation insights.

Subscribe Now

If you'd like to automatically logout users after a period of inactivity, here's a concise guide to set up this feature.

Capture the User Login Time

Begin by recording the time when a user successfully logs in. This step is crucial for tracking the duration of the user session. In the auth-success event of the start screen, store the current time in a custom token:

custom_tokens["user_login_time"] = time();

Determine Session Duration

Next, configure your app to evaluate the length of the user session. This check should occur each time a page is loaded, ensuring continuous monitoring of user activity. Utilize the before-process event in the app configuration for this purpose.

Implement Logout After 30 Minutes

Set a timeout threshold duration as necessary. For this example, we will set it at 30 minutes or 1800 seconds. If the time elapsed since the user's login exceeds this limit, logout the user.

$timeoutDuration = 1800; // 30 minutes in seconds
if (isset(custom_tokens["user_login_time"])) {
    $timeSinceLogin = time() - custom_tokens["user_login_time"];
    if ($timeSinceLogin > $timeoutDuration) {
        location = "?pf-context=pf-logout";
    }
}

Enhancement: Logout only if the user is inactive

To adjust the logout feature to trigger after a period of inactivity rather than time since login, follow these brief modifications. This approach ensures users are only logged out after a designated period of inactivity, making sure that the active users are not disrupted.

Update the auth-success event to track the last active time

custom_tokens["user_last_activity_time"] = time();

Before-Process Event Update

Modify the session check in the before-process event to logout users after inactivity.

if(is_logged_in){
    $timeoutDuration = 1800; // 30 minutes in seconds
    if (isset(custom_tokens["user_last_activity_time"])) {
        $timeSinceLogin = time() - custom_tokens["user_last_activity_time"];
        if ($timeSinceLogin > $timeoutDuration) {
            location = "?pf-context=pf-logout";
        }
    }
    custom_tokens["user_last_activity_time"] = time();
}

By following these steps, you can effectively manage session timeouts in your ProcFu App, enhancing both security and user experience.

Love using ProcFu Guide?

Share your testimonial and let us know how ProcFu has helped you.

Share Your Testimonial

Built with ❤️ by Thaha for the Community