Middleware to automatically authenticate user in Filament

namespace App\Filament\Http\Middleware;
 
use Filament\Facades\Filament;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\RedirectResponse;
 
class AuthenticateDemoUser extends Middleware
{
protected function authenticate($request, array $guards): ?RedirectResponse
{
$guard = Filament::auth();
 
if ($guard->check()) {
return null;
}
 
$this->auth->shouldUse(Filament::getAuthGuard());
 
Filament::auth()->attempt(
$this->getCredentialsFromConfig(),
remember: true
);
 
session()->regenerate();
 
return to_route(config('demo.route'));
}
 
protected function getCredentialsFromConfig(): array
{
return [
'email' => config('demo.user.email'),
'password' => config('demo.user.password'),
];
}
}
namespace App\Filament\Http\Middleware;
 
use Filament\Facades\Filament;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\RedirectResponse;
 
class AuthenticateDemoUser extends Middleware
{
protected function authenticate($request, array $guards): ?RedirectResponse
{
$guard = Filament::auth();
 
if ($guard->check()) {
return null;
}
 
$this->auth->shouldUse(Filament::getAuthGuard());
 
Filament::auth()->attempt(
$this->getCredentialsFromConfig(),
remember: true
);
 
session()->regenerate();
 
return to_route(config('demo.route'));
}
 
protected function getCredentialsFromConfig(): array
{
return [
'email' => config('demo.user.email'),
'password' => config('demo.user.password'),
];
}
}