Skip to content

Commit f061b9f

Browse files
committed
Adding a bit more refactor
1 parent adb4c73 commit f061b9f

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

app/Http/Controllers/Auth/AuthenticatedSessionController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Http\RedirectResponse;
99
use Illuminate\Http\Request;
1010
use Illuminate\Support\Facades\Auth;
11+
use Illuminate\Support\Facades\Hash;
1112
use Illuminate\Support\Facades\Route;
1213
use Inertia\Inertia;
1314
use Inertia\Response;
@@ -30,17 +31,16 @@ public function create(Request $request): Response
3031
*/
3132
public function store(LoginRequest $request): RedirectResponse
3233
{
33-
// Find user by email
3434
$user = User::where('email', $request->email)->first();
3535

36-
// If user exists, password is correct, and 2FA is enabled, redirect to challenge
37-
if ($user && $user->two_factor_confirmed_at && \Illuminate\Support\Facades\Hash::check($request->password, $user->password)) {
36+
// If this user exists, password is correct, and 2FA is enabled, we want to redirect to the 2FA challenge
37+
if ($user && $user->two_factor_confirmed_at && Hash::check($request->password, $user->password)) {
3838
$request->session()->put('login.id', $user->getKey());
3939
// Optionally clear any previous errors
4040
return redirect()->route('two-factor.challenge');
4141
}
4242

43-
// Proceed with normal authentication (this will handle errors and login)
43+
// Otherwise, proceed with normal authentication
4444
$request->authenticate();
4545
$request->session()->regenerate();
4646

app/Http/Controllers/Auth/TwoFactorAuthChallengeController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class TwoFactorAuthChallengeController extends Controller
2121
*/
2222
public function create(Request $request)
2323
{
24+
dd('here');
2425
// Session check is now handled by the EnsureTwoFactorChallengeSession middleware
2526
return Inertia::render('auth/two-factor-challenge');
2627
}
@@ -84,17 +85,17 @@ protected function authenticateUsingCode(Request $request, User $user)
8485
protected function authenticateUsingRecoveryCode(Request $request, User $user)
8586
{
8687
$recoveryCodes = json_decode(decrypt($user->two_factor_recovery_codes), true);
87-
$provided = $request->recovery_code;
88-
$match = collect($recoveryCodes)->first(function ($code) use ($provided) {
89-
return hash_equals($code, $provided);
88+
$providedRecoveryCode = $request->recovery_code;
89+
$matchedRecoveryCode = collect($recoveryCodes)->first(function ($code) use ($providedRecoveryCode) {
90+
return hash_equals($code, $providedRecoveryCode);
9091
});
9192

92-
if (! $match) {
93+
if (! $matchedRecoveryCode) {
9394
return back()->withErrors(['recovery_code' => __('The provided two factor authentication recovery code was invalid.')]);
9495
}
9596

9697
// Remove used recovery code using the ProcessRecoveryCode action
97-
$updatedCodes = app(ProcessRecoveryCode::class)($recoveryCodes, $match);
98+
$updatedCodes = app(ProcessRecoveryCode::class)($recoveryCodes, $matchedRecoveryCode);
9899
if ($updatedCodes === false) {
99100
return back()->withErrors(['recovery_code' => __('The provided two factor authentication recovery code was invalid.')]);
100101
}

routes/auth.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@
5858

5959
// Two-factor challenge routes with the ensure-two-factor-challenge-session middleware
6060
Route::middleware('ensure-two-factor-challenge-session')->group(function () {
61-
Route::get('two-factor-challenge', [TwoFactorAuthChallengeController::class, 'create'])
62-
->name('two-factor.challenge');
63-
61+
Route::inertia('two-factor-challenge', 'auth/two-factor-challenge')->name('two-factor.challenge');
6462
Route::post('two-factor-challenge', [TwoFactorAuthChallengeController::class, 'store']);
6563
});

0 commit comments

Comments
 (0)