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

[Feature] Validate invalid OTP #24

Merged
merged 4 commits into from
Aug 28, 2024
Merged
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
24 changes: 20 additions & 4 deletions src/Pages/TwoFactor.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Laravel\Fortify\Actions\ConfirmTwoFactorAuthentication;
use Laravel\Fortify\Actions\DisableTwoFactorAuthentication;
use Laravel\Fortify\Actions\EnableTwoFactorAuthentication;
Expand Down Expand Up @@ -178,10 +179,21 @@ public function confirmAction(): Action
->label(__('Confirm'))
->color('primary')
->action(function ($data) {
if (count($this->otpCodeData) === 0) {
$this->throwFailureValidationException();
}

$this->confirmTwoFactorAuthentication(app(ConfirmTwoFactorAuthentication::class));
});
}

protected function throwFailureValidationException(): never
{
throw ValidationException::withMessages([
'otpCodeData.code' => __('The code you entered is invalid.'),
]);
}

public function regenerateAction(): Action
{
return Action::make('regenerate')
Expand Down Expand Up @@ -227,11 +239,15 @@ public function enableTwoFactorAuthentication(EnableTwoFactorAuthentication $ena

public function confirmTwoFactorAuthentication(ConfirmTwoFactorAuthentication $confirm): void
{
$confirm($this->user, $this->otpCodeData['code']);
try {
$confirm($this->user, $this->otpCodeData['code']);

$this->showingQrCode = false;
$this->showingConfirmation = false;
$this->showingRecoveryCodes = true;
$this->showingQrCode = false;
$this->showingConfirmation = false;
$this->showingRecoveryCodes = true;
} catch (\Exception $e) {
$this->throwFailureValidationException();
}
}

public function disableTwoFactorAuthentication(DisableTwoFactorAuthentication $disable): void
Expand Down