Skip to content

Commit 33f8af0

Browse files
authored
[1.x] Add TwoFactorAuthenticationFailed event (#558)
* Add two factor authentication failed event * Add new line * styleci * styleci * extends TwoFactorAuthenticationEvent
1 parent 6761f04 commit 33f8af0

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Laravel\Fortify\Events;
4+
5+
class TwoFactorAuthenticationFailed extends TwoFactorAuthenticationEvent
6+
{
7+
//
8+
}

src/Http/Controllers/TwoFactorAuthenticatedSessionController.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Laravel\Fortify\Contracts\TwoFactorChallengeViewResponse;
1010
use Laravel\Fortify\Contracts\TwoFactorLoginResponse;
1111
use Laravel\Fortify\Events\RecoveryCodeReplaced;
12+
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
1213
use Laravel\Fortify\Http\Requests\TwoFactorLoginRequest;
1314

1415
class TwoFactorAuthenticatedSessionController extends Controller
@@ -61,6 +62,8 @@ public function store(TwoFactorLoginRequest $request)
6162

6263
event(new RecoveryCodeReplaced($user, $code));
6364
} elseif (! $request->hasValidCode()) {
65+
event(new TwoFactorAuthenticationFailed($user));
66+
6467
return app(FailedTwoFactorLoginResponse::class)->toResponse($request);
6568
}
6669

tests/AuthenticatedSessionControllerWithTwoFactorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Event;
99
use Illuminate\Support\Facades\Hash;
1010
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
11+
use Laravel\Fortify\Events\TwoFactorAuthenticationFailed;
1112
use Laravel\Fortify\Features;
1213
use Laravel\Fortify\Tests\Models\UserWithTwoFactor;
1314
use Orchestra\Testbench\Attributes\DefineEnvironment;
@@ -200,6 +201,8 @@ public function test_two_factor_authentication_preserves_remember_me_selection()
200201

201202
public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
202203
{
204+
Event::fake();
205+
203206
// Setting window to 0 should mean any old OTP is instantly invalid
204207
Features::twoFactorAuthentication(['window' => 0]);
205208

@@ -222,6 +225,8 @@ public function test_two_factor_challenge_fails_for_old_otp_and_zero_window()
222225
'code' => $previousOtp,
223226
]);
224227

228+
Event::assertDispatched(TwoFactorAuthenticationFailed::class);
229+
225230
$response->assertRedirect('/two-factor-challenge')
226231
->assertSessionHas('login.id')
227232
->assertSessionHasErrors(['code']);

0 commit comments

Comments
 (0)