How to Check User Login, Online Status & Last Seen in Laravel 10

How to Check User Login, Online Status & Last Seen in Laravel 10

In any web application that requires user authentication, it’s important to know the user’s login status, online status, and last seen time. In this article, we will explore how to implement a system to check user login, online status, and last seen time in Laravel 10.

Prerequisites

Before getting started, you should have the following installed:

  • Laravel 10.x
  • Composer
  • Node.js
  • NPM

Step 1: Create a User Model

First, we need to create a User model. You can do this by running the following command in your terminal:

php artisan make:model User -m

This will create a new User model with a migration file to create the users table in your database.

Step 2: Update the Users Table

Open the migration file created in the previous step and add the following columns to the users table:

$table->boolean('is_online')->default(false);
$table->timestamp('last_seen')->nullable();

This will add a boolean column to track the user’s online status and a timestamp column to track the user’s last seen time.

Run the migration to update the users table by running the following command in your terminal:

php artisan migrate

Step 3: Add Online Status Middleware

Create a new middleware using the following command:

php artisan make:middleware OnlineStatusMiddleware

In your newly created middleware, add the following code:

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class OnlineStatusMiddleware
{
    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        if ($user) {
            $expiresAt = now()->addMinutes(5);

            Cache::put('user-is-online-' . $user->id, true, $expiresAt);
            Cache::put('user-last-seen-' . $user->id, now(), $expiresAt);

            $user->is_online = true;
            $user->last_seen = now();
            $user->save();
        }

        return $next($request);
    }
}

This code will set the user’s online status to true and update their last seen time whenever they make a request to your application.

Step 4: Register the Middleware

Add the OnlineStatusMiddleware to your application’s middleware stack by adding the following code to the $middlewareGroups property in your app/Http/Kernel.php file:

'web' => [
    // ...
    \App\Http\Middleware\OnlineStatusMiddleware::class,
],

Step 5: Check User Status

To check the user’s online status and last seen time, add the following code to your User model:

public function isOnline()
{
    return Cache::has('user-is-online-' . $this->id);
}

public function lastSeen()
{
    if ($this->isOnline()) {
        return 'Online';
    }

    $lastSeen = Cache::get('user-last-seen-' . $this->id);

    if ($lastSeen) {
        return $lastSeen->diffForHumans();
    }

    return 'N/A';
}

This code will check if the user is currently online and return their last seen time in a human-readable format.

Step 6: Display User Status

To display the user’s online status and last seen time, you can use the following code in your views:

@if ($user->isOnline())
    <span class="text-success">Online</span>
@else
    <span class="text-muted">{{ $user->lastSeen() }}</span>
@endif

This code will display “Online” if the user is currently online, or their last seen time in a muted text if they’re offline.

Step 7: Test the User Status

To test the user status, log in with a user account and visit a page in your application. Then, check the users table in your database to see if the user’s online status and last seen time have been updated. You can also check the user status on your application’s frontend by visiting a page that displays the user’s online status and last seen time.

Conclusion

In this article, we learned how to implement a system to check user login, online status, and last seen time in Laravel 10. By following the steps outlined in this article, you should now be able to add this functionality to your own Laravel applications. This feature can help improve user engagement and provide a better user experience.

This Article Ideas has been taken from the following websites; which are following:

How to Check User Login, Online Status & Last Seen in Laravel 10

Laravel 9 Check User Login, Online Status & Last Seen

https://www.nicesnippets.com/blog/how-to-check-user-online-status-last-seen-in-laravel\

https://www.golinuxcloud.com/check-user-online-or-offline-on-laravel/

https://www.itsolutionstuff.com/post/how-to-get-online-users-in-laravelexample.html

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *