Skip to content

Commit

Permalink
[2.x] Dispatch Event when Two Factor Challenged (laravel#239)
Browse files Browse the repository at this point in the history
* dispatch event when two factor challenged and test

* use statements styleci

(cherry picked from commit b490728)
  • Loading branch information
danclaytondev authored and Joel Butcher committed Jan 7, 2022
1 parent 70ea090 commit db69348
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Actions/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Auth\Events\Failed;
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Fortify;
use Laravel\Fortify\LoginRateLimiter;
use Laravel\Fortify\TwoFactorAuthenticatable;
Expand Down Expand Up @@ -132,6 +133,8 @@ protected function twoFactorChallengeResponse($request, $user)
'login.remember' => $request->filled('remember'),
]);

TwoFactorAuthenticationChallenged::dispatch($user);

return $request->wantsJson()
? response()->json(['two_factor' => true])
: redirect()->route('two-factor.login');
Expand Down
8 changes: 8 additions & 0 deletions src/Events/TwoFactorAuthenticationChallenged.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Laravel\Fortify\Events;

class TwoFactorAuthenticationChallenged extends TwoFactorAuthenticationEvent
{
//
}
6 changes: 6 additions & 0 deletions tests/AuthenticatedSessionControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Auth\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Laravel\Fortify\Contracts\LoginViewResponse;
use Laravel\Fortify\Events\TwoFactorAuthenticationChallenged;
use Laravel\Fortify\Features;
use Laravel\Fortify\FortifyServiceProvider;
use Laravel\Fortify\LoginRateLimiter;
Expand Down Expand Up @@ -48,6 +50,8 @@ public function test_user_can_authenticate()

public function test_user_is_redirected_to_challenge_when_using_two_factor_authentication()
{
Event::fake();

app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);

$this->loadLaravelMigrations(['--database' => 'testbench']);
Expand All @@ -69,6 +73,8 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe
]);

$response->assertRedirect('/two-factor-challenge');

Event::assertDispatched(TwoFactorAuthenticationChallenged::class);
}

public function test_user_can_authenticate_when_two_factor_challenge_is_disabled()
Expand Down

0 comments on commit db69348

Please sign in to comment.