mailwolt/app/Livewire/Ui/System/Modal/TotpSetupModal.php

55 lines
1.5 KiB
PHP

<?php
namespace App\Livewire\Ui\System\Modal;
use App\Services\TotpService;
use Illuminate\Support\Facades\Auth;
use LivewireUI\Modal\ModalComponent;
class TotpSetupModal extends ModalComponent
{
public string $step = 'scan'; // scan → verify → codes
public string $code = '';
public string $secret = '';
public array $recoveryCodes = [];
public string $qrSvg = '';
public static function modalMaxWidth(): string { return 'md'; }
public function mount(): void
{
$totp = app(TotpService::class);
$this->secret = $totp->generateSecret();
$this->qrSvg = $totp->qrCodeSvg(Auth::user(), $this->secret);
}
public function verify(): void
{
$this->validate(['code' => 'required|digits:6']);
$totp = app(TotpService::class);
if (!$totp->verify($this->secret, $this->code)) {
$this->addError('code', 'Ungültiger Code. Bitte erneut versuchen.');
return;
}
$this->recoveryCodes = $totp->enable(Auth::user(), $this->secret);
$this->step = 'codes';
}
public function done(): void
{
$this->dispatch('toast', type: 'done', badge: '2FA',
title: 'TOTP aktiviert',
text: 'Zwei-Faktor-Authentifizierung ist jetzt aktiv.', duration: 5000);
$this->dispatch('2fa-status-changed');
$this->closeModal();
}
public function render()
{
return view('livewire.ui.system.modal.totp-setup-modal');
}
}