Skip to content

Commit

Permalink
[1.x] Ensure logout route is authenticated (#536)
Browse files Browse the repository at this point in the history
* Ensure logout route is authenticated

* Formatting

* Remove unused user
  • Loading branch information
timacdonald committed May 8, 2024
1 parent 9da961e commit a725684
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
1 change: 1 addition & 0 deletions routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
]));

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

// Password Reset...
Expand Down
28 changes: 28 additions & 0 deletions tests/AuthenticatedSessionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Laravel\Fortify\Tests;

use Illuminate\Auth\Events\Logout;
use Illuminate\Cache\RateLimiter;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Auth\User;
Expand Down Expand Up @@ -404,6 +405,33 @@ public function test_case_insensitive_usernames_can_be_used()
$response->assertRedirect('/home');
}

public function test_users_can_logout(): void
{
$user = TestAuthenticationSessionUser::forceCreate([
'name' => 'Taylor Otwell',
'email' => 'taylor@laravel.com',
'password' => bcrypt('secret'),
]);
Event::fake([Logout::class]);

$response = $this->actingAs($user)->post('/logout');

$response->assertRedirect();
$this->assertGuest();
Event::assertDispatched(fn (Logout $logout) => $logout->user->is($user));
}

public function test_must_be_authenticated_to_logout(): void
{
Event::fake([Logout::class]);

$response = $this->post('/logout');

$response->assertRedirect();
$this->assertGuest();
Event::assertNotDispatched(Logout::class);
}

protected function defineEnvironment($app)
{
parent::defineEnvironment($app);
Expand Down

0 comments on commit a725684

Please sign in to comment.