|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Livewire\Settings; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Laravel\Fortify\Actions\ConfirmTwoFactorAuthentication; |
| 7 | +use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; |
| 8 | +use Laravel\Fortify\Actions\EnableTwoFactorAuthentication; |
| 9 | +use Laravel\Fortify\Features; |
| 10 | +use Laravel\Fortify\Fortify; |
| 11 | +use Livewire\Attributes\Locked; |
| 12 | +use Livewire\Attributes\Validate; |
| 13 | +use Livewire\Component; |
| 14 | +use Symfony\Component\HttpFoundation\Response; |
| 15 | + |
| 16 | +class TwoFactor extends Component |
| 17 | +{ |
| 18 | + #[Locked] |
| 19 | + public bool $twoFactorEnabled; |
| 20 | + |
| 21 | + #[Locked] |
| 22 | + public bool $requiresConfirmation; |
| 23 | + |
| 24 | + #[Locked] |
| 25 | + public string $qrCodeSvg = ''; |
| 26 | + |
| 27 | + #[Locked] |
| 28 | + public string $manualSetupKey = ''; |
| 29 | + |
| 30 | + public bool $showModal = false; |
| 31 | + |
| 32 | + public bool $showVerificationStep = false; |
| 33 | + |
| 34 | + #[Validate('required|string|size:6', onUpdate: false)] |
| 35 | + public string $code = ''; |
| 36 | + |
| 37 | + /** |
| 38 | + * Mount the component. |
| 39 | + */ |
| 40 | + public function mount(DisableTwoFactorAuthentication $disableTwoFactorAuthentication): void |
| 41 | + { |
| 42 | + abort_unless(Features::enabled(Features::twoFactorAuthentication()), Response::HTTP_FORBIDDEN); |
| 43 | + |
| 44 | + if (Fortify::confirmsTwoFactorAuthentication() && is_null(auth()->user()->two_factor_confirmed_at)) { |
| 45 | + $disableTwoFactorAuthentication(auth()->user()); |
| 46 | + } |
| 47 | + |
| 48 | + $this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication(); |
| 49 | + $this->requiresConfirmation = Features::optionEnabled(Features::twoFactorAuthentication(), 'confirm'); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Enable two-factor authentication for the user. |
| 54 | + */ |
| 55 | + public function enable(EnableTwoFactorAuthentication $enableTwoFactorAuthentication): void |
| 56 | + { |
| 57 | + $enableTwoFactorAuthentication(auth()->user()); |
| 58 | + |
| 59 | + if (! $this->requiresConfirmation) { |
| 60 | + $this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication(); |
| 61 | + } |
| 62 | + |
| 63 | + $this->loadSetupData(); |
| 64 | + |
| 65 | + $this->showModal = true; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Load the two-factor authentication setup data for the user. |
| 70 | + */ |
| 71 | + private function loadSetupData(): void |
| 72 | + { |
| 73 | + $user = auth()->user(); |
| 74 | + |
| 75 | + try { |
| 76 | + $this->qrCodeSvg = $user?->twoFactorQrCodeSvg(); |
| 77 | + $this->manualSetupKey = decrypt($user->two_factor_secret); |
| 78 | + } catch (Exception) { |
| 79 | + $this->addError('setupData', 'Failed to fetch setup data.'); |
| 80 | + |
| 81 | + $this->reset('qrCodeSvg', 'manualSetupKey'); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Show the two-factor verification step if necessary. |
| 87 | + */ |
| 88 | + public function showVerificationIfNecessary(): void |
| 89 | + { |
| 90 | + if ($this->requiresConfirmation) { |
| 91 | + $this->showVerificationStep = true; |
| 92 | + |
| 93 | + $this->resetErrorBag(); |
| 94 | + |
| 95 | + return; |
| 96 | + } |
| 97 | + |
| 98 | + $this->closeModal(); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Confirm two-factor authentication for the user. |
| 103 | + */ |
| 104 | + public function confirmTwoFactor(ConfirmTwoFactorAuthentication $confirmTwoFactorAuthentication): void |
| 105 | + { |
| 106 | + $this->validate(); |
| 107 | + |
| 108 | + $confirmTwoFactorAuthentication(auth()->user(), $this->code); |
| 109 | + |
| 110 | + $this->closeModal(); |
| 111 | + |
| 112 | + $this->twoFactorEnabled = true; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Reset two-factor verification state. |
| 117 | + */ |
| 118 | + public function resetVerification(): void |
| 119 | + { |
| 120 | + $this->reset('code', 'showVerificationStep'); |
| 121 | + |
| 122 | + $this->resetErrorBag(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Disable two-factor authentication for the user. |
| 127 | + */ |
| 128 | + public function disable(DisableTwoFactorAuthentication $disableTwoFactorAuthentication): void |
| 129 | + { |
| 130 | + $disableTwoFactorAuthentication(auth()->user()); |
| 131 | + |
| 132 | + $this->twoFactorEnabled = false; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Close the two-factor authentication modal. |
| 137 | + */ |
| 138 | + public function closeModal(): void |
| 139 | + { |
| 140 | + $this->reset( |
| 141 | + 'code', |
| 142 | + 'manualSetupKey', |
| 143 | + 'qrCodeSvg', |
| 144 | + 'showModal', |
| 145 | + 'showVerificationStep', |
| 146 | + ); |
| 147 | + |
| 148 | + $this->resetErrorBag(); |
| 149 | + |
| 150 | + if (! $this->requiresConfirmation) { |
| 151 | + $this->twoFactorEnabled = auth()->user()->hasEnabledTwoFactorAuthentication(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Get the current modal configuration state. |
| 157 | + */ |
| 158 | + public function getModalConfigProperty(): array |
| 159 | + { |
| 160 | + if ($this->twoFactorEnabled) { |
| 161 | + return [ |
| 162 | + 'title' => __('Two-Factor Authentication Enabled'), |
| 163 | + 'description' => __('Two-factor authentication is now enabled. Scan the QR code or enter the setup key in your authenticator app.'), |
| 164 | + 'buttonText' => __('Close'), |
| 165 | + ]; |
| 166 | + } |
| 167 | + |
| 168 | + if ($this->showVerificationStep) { |
| 169 | + return [ |
| 170 | + 'title' => __('Verify Authentication Code'), |
| 171 | + 'description' => __('Enter the 6-digit code from your authenticator app.'), |
| 172 | + 'buttonText' => __('Continue'), |
| 173 | + ]; |
| 174 | + } |
| 175 | + |
| 176 | + return [ |
| 177 | + 'title' => __('Enable Two-Factor Authentication'), |
| 178 | + 'description' => __('To finish enabling two-factor authentication, scan the QR code or enter the setup key in your authenticator app.'), |
| 179 | + 'buttonText' => __('Continue'), |
| 180 | + ]; |
| 181 | + } |
| 182 | +} |
0 commit comments