Skip to content

Commit

Permalink
[Feature] Validate invalid OTP (#24)
Browse files Browse the repository at this point in the history
* Validate otp

* Fix styling

* Remove return

* Fix styling

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Aug 28, 2024
1 parent fb69b4a commit ab9465d
Showing 1 changed file with 20 additions and 4 deletions.
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

0 comments on commit ab9465d

Please sign in to comment.