How do I detect logout when the browser closes or when the session closes (timeout reached, inactivity, etc.) in Laravel 8?

Asked

Viewed 31 times

0

I am trying to change the default behavior of the Illuminate Session Cookiesessionhandler class so that when the Destroy method is invoked it is registered in the database when a user session has been terminated. As this class is in the vendor directory and this is rewritten for each update of the framework, I decided to create a new class and extend the one I want to modify, so I overwritten the Destroy method, and also created a Provider similar to the one indicated in documentation, however I did not have the expected result, could someone tell me if there is an easier way to register when the user session ends up inactivity or have closed the browser?. Follow the files modified in the process, thank you very much:

Provider class

class CookieSessionProvider extends SessionServiceProvider
{
    /**
     * The container instance.
     *
     * @var \Illuminate\Contracts\Container\Container
     */
    protected $container;
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Session::extend('customCookie', function ($app) {
            // Return an implementation of SessionHandlerInterface...
            return new CustomCookieSessionHandler($this->app->make('cookie'), config('session.lifetime'));
        });
    }
}

Handler class

<?php


namespace SIICA\Extensions;

    
class CustomCookieSessionHandler extends CookieSessionHandler
{
    public function destroy($sessionId)
    {
        event(new UserLogoutEvent(Auth::user()));
        return parent::destroy($sessionId);
    }
}

Change in . env

SESSION_DRIVER=customCookie
SESSION_LIFETIME=120

Changes to config/Session.php

  'customCookie' => env(
        'SESSION_COOKIE',
        Str::slug(env('APP_NAME', 'laravel'), '_').'_session'
    ),

Changes to config/app.php

'providers' => [
 //Outros providers omitidos
 SIICA\Providers\Custom\CookieSessionProvider::class,
    ],

Handling of logout event

class UserLogoutListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Handle the event.
     *
     * @param  object  $event
     * @return void
     */
    public function handle(UserLogoutEvent $event)
    {
        $logoutDateTime = new \DateTime();
        $userAccess = UserAccess::where('user_id', $event->user->id)
            ->whereNull('logout_at')->first();
        if (!empty($userAccess)){
            $userAccess->logout_at = $logoutDateTime->format(MYSQL_FORMAT_DATETIME);
            $userAccess->save();
        }

    }
}
  • There is no way to intercept this browser lock, you could create a command q test users q were marked in the database as authenticated and the logout record is null if there is an authenticated user cache entry, and schedule p run every minute. To create the cache, in the user login, record the login information in the database, and also a Cache with the user id and the cache expiration time at each request. Let me know if you need a code for that

  • Thank you friend, if you can contribute an example code thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.