82 lines
2.5 KiB
PHP
82 lines
2.5 KiB
PHP
<?php
|
||
|
||
namespace App\Livewire\Ui\Security\Modal;
|
||
|
||
use App\Livewire\Ui\Security\Notification;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use Illuminate\Support\Facades\Cache;
|
||
use LivewireUI\Modal\ModalComponent;
|
||
|
||
class Email2faSetupModal extends ModalComponent
|
||
{
|
||
public string $code = '';
|
||
public bool $alreadyActive = false;
|
||
public int $cooldown = 0; // Sek. bis zum nächsten Versand
|
||
|
||
public static function modalMaxWidth(): string { return 'md'; }
|
||
|
||
public function mount(): void
|
||
{
|
||
$u = Auth::user();
|
||
$this->alreadyActive = (bool) ($u->two_factor_email_enabled ?? false);
|
||
}
|
||
|
||
public function sendMail(): void
|
||
{
|
||
if ($this->cooldown > 0) return;
|
||
|
||
$u = Auth::user();
|
||
$pin = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
||
|
||
// 10 Minuten gültig (Cache-Key pro User)
|
||
Cache::put("email-2fa:setup:{$u->id}", password_hash($pin, PASSWORD_DEFAULT), now()->addMinutes(10));
|
||
|
||
// sehr einfache Notification – ersetze durch Mailables/Markdown:
|
||
Notification::route('mail', $u->email)->notify(new \App\Notifications\PlainTextNotification(
|
||
subject: 'Dein E-Mail-2FA Code',
|
||
lines: [
|
||
"Dein Bestätigungscode lautet: **{$pin}**",
|
||
'Der Code ist 10 Minuten gültig.',
|
||
],
|
||
));
|
||
|
||
$this->cooldown = 30;
|
||
$this->dispatch('toast', body: 'Code gesendet.');
|
||
$this->dispatch('tick-down'); // optionaler JS-Timer
|
||
}
|
||
|
||
public function verifyAndEnable(): void
|
||
{
|
||
$u = Auth::user();
|
||
$hash = Cache::get("email-2fa:setup:{$u->id}");
|
||
if (!$hash || !password_verify(preg_replace('/\D/', '', $this->code), $hash)) {
|
||
$this->dispatch('toast', body: 'Code ungültig oder abgelaufen.');
|
||
return;
|
||
}
|
||
|
||
$u->two_factor_email_enabled = true; // bool Spalte auf users
|
||
$u->save();
|
||
Cache::forget("email-2fa:setup:{$u->id}");
|
||
|
||
$this->dispatch('email2fa-enabled');
|
||
$this->dispatch('toast', body: 'E-Mail-2FA aktiviert.');
|
||
$this->dispatch('closeModal');
|
||
}
|
||
|
||
public function disable(): void
|
||
{
|
||
$u = Auth::user();
|
||
$u->two_factor_email_enabled = false;
|
||
$u->save();
|
||
|
||
$this->dispatch('email2fa-disabled');
|
||
$this->dispatch('toast', body: 'E-Mail-2FA deaktiviert.');
|
||
$this->dispatch('closeModal');
|
||
}
|
||
|
||
public function render()
|
||
{
|
||
return view('livewire.ui.security.modal.email2fa-setup-modal');
|
||
}
|
||
}
|