Skip to content

Commit 596e6a8

Browse files
authored
Only use two factor action when enabled (#127)
This resolves the issue of validating credentials twice when two-factor is disabled.
1 parent c77b183 commit 596e6a8

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Diff for: src/Http/Controllers/AuthenticatedSessionController.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Laravel\Fortify\Contracts\LoginResponse;
1414
use Laravel\Fortify\Contracts\LoginViewResponse;
1515
use Laravel\Fortify\Contracts\LogoutResponse;
16+
use Laravel\Fortify\Features;
1617
use Laravel\Fortify\Fortify;
1718
use Laravel\Fortify\Http\Requests\LoginRequest;
1819

@@ -82,7 +83,7 @@ protected function loginPipeline(LoginRequest $request)
8283

8384
return (new Pipeline(app()))->send($request)->through(array_filter([
8485
config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
85-
RedirectIfTwoFactorAuthenticatable::class,
86+
Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null,
8687
AttemptToAuthenticate::class,
8788
PrepareAuthenticatedSession::class,
8889
]));

Diff for: tests/AuthenticatedSessionControllerTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Support\Facades\Auth;
88
use Illuminate\Support\Facades\Schema;
99
use Laravel\Fortify\Contracts\LoginViewResponse;
10+
use Laravel\Fortify\Features;
1011
use Laravel\Fortify\FortifyServiceProvider;
1112
use Laravel\Fortify\LoginRateLimiter;
1213
use Laravel\Fortify\TwoFactorAuthenticatable;
@@ -70,6 +71,37 @@ public function test_user_is_redirected_to_challenge_when_using_two_factor_authe
7071
$response->assertRedirect('/two-factor-challenge');
7172
}
7273

74+
public function test_user_can_authenticate_when_two_factor_challenge_is_disabled()
75+
{
76+
app('config')->set('auth.providers.users.model', TestTwoFactorAuthenticationSessionUser::class);
77+
78+
$features = app('config')->get('fortify.features');
79+
80+
unset($features[array_search(Features::twoFactorAuthentication(), $features)]);
81+
82+
app('config')->set('fortify.features', $features);
83+
84+
$this->loadLaravelMigrations(['--database' => 'testbench']);
85+
86+
Schema::table('users', function ($table) {
87+
$table->text('two_factor_secret')->nullable();
88+
});
89+
90+
TestTwoFactorAuthenticationSessionUser::forceCreate([
91+
'name' => 'Taylor Otwell',
92+
'email' => '[email protected]',
93+
'password' => bcrypt('secret'),
94+
'two_factor_secret' => 'test-secret',
95+
]);
96+
97+
$response = $this->withoutExceptionHandling()->post('/login', [
98+
'email' => '[email protected]',
99+
'password' => 'secret',
100+
]);
101+
102+
$response->assertRedirect('/home');
103+
}
104+
73105
public function test_validation_exception_returned_on_failure()
74106
{
75107
$this->loadLaravelMigrations(['--database' => 'testbench']);

0 commit comments

Comments
 (0)