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

[2.x] Dispatch Event when Two Factor Challenged #239

Merged
merged 2 commits into from
Mar 17, 2021
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
3 changes: 3 additions & 0 deletions src/Actions/RedirectIfTwoFactorAuthenticatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Auth\StatefulGuard;
use Illuminate\Support\Facades\Hash;
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 @@ -133,6 +134,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