Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kamalpal's avatar

update users last activity

I am using Laravel 5.2 with JWT Authentication, and need to know whether the user is online or not, for that I have created an extra field in user's table "last_activity_at" which would be updated as per user's activity.

The problem is JWT Auth Middleware is in /vendor directory, where should I put my code to update user's table for last activity ?

0 likes
11 replies
tisuchi's avatar

Can think of it in different way... Just add a new column in your user table called isOnline. Now, if a user loggedin in your system, make it 1 and during logged out, 0.

In order to check whether user is online or not, check isOnline column... thats all.

kamalpal's avatar

@tisuchi what if user clear browser cookies ? logout won't triggered and user will shown as logged in until user login again and logout using application.

However I am using JWT Authentication, It's stateless.

Snapey's avatar

you can't do anything that relies on user logging out

kamalpal's avatar

@Snapey Yes, due to that I am trying to store last activity timestamp in users table, and will calculate difference from current timestamp, and if it's less than five minutes, user is online.

1 like
Snapey's avatar

If its any activity, then write your own before (or after) middleware and log at that point?

kamalpal's avatar

@Snapey Yes, this is I am confused about, where should I put that update statement ? Thanks

Spillmester's avatar
Level 9

php artisan make:middleware UserActive php artisan make:migration AddLastActiveField composer dump-autoload -o

In folder app\http\Middleware\UserActive.php

public function handle($request, Closure $next)
{
    if (\Auth::check()) {
        // The user is logged in...
        $user = \Auth::user();
        $user->lastActive = date('Y-m-d H:i:s');
        $user->save();
    }
    return $next($request);
}

In the migration file

public function up() { // Schema::table('users', function (Blueprint $table) { $table->timestamp('lastActive'); }); }

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    //
    Schema::table('users', function (Blueprint $table) {
       $table->dropColumn('lastActive');
    });
}

Looking back lastActive should probably be last_active as that is the common way to name mysql columns. Add the middleware to app\http\Kernel.php

protected $middlewareGroups = [ 'web' => [ \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \Illuminate\View\Middleware\ShareErrorsFromSession::class, \App\Http\Middleware\VerifyCsrfToken::class, \App\Http\Middleware\UserActive::class ],

    'api' => [
        'throttle:60,1',
    ],
];

more info on middleware https://laracasts.com/series/intermediate-laravel/episodes/7

2 likes
jsonuk's avatar

@adw[email protected] this works without updating the update_at stamp

I find this preferable because I want to know when the record has been changed and not synced with the activity stamp.

    public function handle($request, Closure $next)
    {
        if (\Auth::check()) {
            \DB::table('users')
                ->where('id', \Auth::id())
                ->update(['last_activity' => \Carbon\Carbon::now()]);
        }
        return $next($request);
    }
1 like

Please or to participate in this conversation.