<?php
namespace App\Listeners;

use Illuminate\Auth\Events\Logout;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Session;

class ClearSessionOnLogout
{
    protected $guard;

    public function __construct(Guard $guard)
    {
        $this->guard = $guard;
    }

    public function handle(Logout $event)
    {
        // Retrieve the user session id
        $sessionId = Session::getId();

        // Delete the session from the database
        if ($event->user) {
            $this->guard->logout();
            Session::getHandler()->destroy($sessionId);
        }
    }
}
