pendingUser($request) === null) { return redirect()->route('login'); } return Inertia::render('auth/two-factor-challenge'); } public function store(Request $request): RedirectResponse { $user = $this->pendingUser($request); if ($user === null) { return redirect()->route('login'); } $request->validate([ 'code' => ['nullable', 'string'], 'recovery_code' => ['nullable', 'string'], ]); $throttleKey = 'two-factor.challenge.'.$user->id; if (RateLimiter::tooManyAttempts($throttleKey, 5)) { throw ValidationException::withMessages([ 'code' => __('auth.throttle', ['seconds' => (string) RateLimiter::availableIn($throttleKey)]), ]); } $valid = $request->filled('code') ? $this->twoFactor->verify($user, (string) $request->string('code')) : ($request->filled('recovery_code') && $this->twoFactor->consumeRecoveryCode($user, trim((string) $request->string('recovery_code')))); if (! $valid) { RateLimiter::hit($throttleKey); throw ValidationException::withMessages([ 'code' => __('The provided two-factor authentication code was invalid.'), ]); } RateLimiter::clear($throttleKey); Auth::login($user, (bool) $request->session()->pull(SignIn::TWO_FACTOR_REMEMBER, false)); $request->session()->forget(SignIn::TWO_FACTOR_ID); $request->session()->regenerate(); return redirect()->intended(route('dashboard', absolute: false)); } private function pendingUser(Request $request): ?User { $id = $request->session()->get(SignIn::TWO_FACTOR_ID); if (! is_int($id) && ! is_string($id)) { return null; } $user = User::query()->find($id); return $user instanceof User && $user->maySignIn() && $user->hasTwoFactorEnabled() ? $user : null; } }