Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.x] Ensure logout route is authenticated #536

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' => '[email protected]',
'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