Skip to content

Commit a725684

Browse files
authored
[1.x] Ensure logout route is authenticated (#536)
* Ensure logout route is authenticated * Formatting * Remove unused user
1 parent 9da961e commit a725684

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

routes/routes.php

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
]));
4343

4444
Route::post(RoutePath::for('logout', '/logout'), [AuthenticatedSessionController::class, 'destroy'])
45+
->middleware([config('fortify.auth_middleware', 'auth').':'.config('fortify.guard')])
4546
->name('logout');
4647

4748
// Password Reset...

tests/AuthenticatedSessionControllerTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Laravel\Fortify\Tests;
44

5+
use Illuminate\Auth\Events\Logout;
56
use Illuminate\Cache\RateLimiter;
67
use Illuminate\Contracts\Auth\Authenticatable;
78
use Illuminate\Foundation\Auth\User;
@@ -404,6 +405,33 @@ public function test_case_insensitive_usernames_can_be_used()
404405
$response->assertRedirect('/home');
405406
}
406407

408+
public function test_users_can_logout(): void
409+
{
410+
$user = TestAuthenticationSessionUser::forceCreate([
411+
'name' => 'Taylor Otwell',
412+
'email' => '[email protected]',
413+
'password' => bcrypt('secret'),
414+
]);
415+
Event::fake([Logout::class]);
416+
417+
$response = $this->actingAs($user)->post('/logout');
418+
419+
$response->assertRedirect();
420+
$this->assertGuest();
421+
Event::assertDispatched(fn (Logout $logout) => $logout->user->is($user));
422+
}
423+
424+
public function test_must_be_authenticated_to_logout(): void
425+
{
426+
Event::fake([Logout::class]);
427+
428+
$response = $this->post('/logout');
429+
430+
$response->assertRedirect();
431+
$this->assertGuest();
432+
Event::assertNotDispatched(Logout::class);
433+
}
434+
407435
protected function defineEnvironment($app)
408436
{
409437
parent::defineEnvironment($app);

0 commit comments

Comments
 (0)