Skip to content

Commit b6f3ed6

Browse files
authored
Ensure missing user does not throw a type error (#366)
1 parent 6c1c174 commit b6f3ed6

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

src/PulseServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ protected function listenForEvents(): void
119119
$this->app->booted(function () {
120120
$this->callAfterResolving(Dispatcher::class, function (Dispatcher $event, Application $app) {
121121
$event->listen(function (Logout $event) use ($app) {
122+
if ($event->user === null) {
123+
return;
124+
}
125+
122126
$pulse = $app->make(Pulse::class);
123127

124128
$pulse->rescue(fn () => $pulse->rememberUser($event->user));

tests/Feature/PulseTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
<?php
22

3+
use Illuminate\Auth\Events\Logout;
34
use Illuminate\Contracts\Auth\Authenticatable;
45
use Illuminate\Support\Collection;
56
use Illuminate\Support\Facades\App;
67
use Illuminate\Support\Facades\Auth;
78
use Illuminate\Support\Facades\Config;
9+
use Illuminate\Support\Facades\Event;
810
use Illuminate\Support\Facades\Facade;
911
use Laravel\Pulse\Contracts\ResolvesUsers;
1012
use Laravel\Pulse\Contracts\Storage;
@@ -246,6 +248,13 @@ public function find(int|string|null $key): object
246248
expect($persistentMiddleware)->not->toContain(MyTestMiddleware::class.':admin');
247249
});
248250

251+
it('handles logout events when there is no user', function () {
252+
// This will throw a type error when unhandled...
253+
Event::dispatch(new Logout('session', user: null));
254+
255+
expect(true)->toBe(true);
256+
});
257+
249258
class MyTestMiddleware
250259
{
251260
//

0 commit comments

Comments
 (0)