57 lines
1.5 KiB
PHP
57 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Auth;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
use PragmaRX\Google2FAQRCode\Google2FA;
|
|
|
|
#[Layout('layouts.auth')]
|
|
#[Title('2FA einrichten — Clusev')]
|
|
class TwoFactorSetup extends Component
|
|
{
|
|
public string $secret = '';
|
|
|
|
#[Validate('required|string')]
|
|
public string $code = '';
|
|
|
|
public function mount()
|
|
{
|
|
if (Auth::user()->hasTwoFactorEnabled()) {
|
|
return $this->redirect(route('dashboard'), navigate: true);
|
|
}
|
|
|
|
$this->secret = (new Google2FA)->generateSecretKey();
|
|
}
|
|
|
|
public function confirm()
|
|
{
|
|
$this->validate();
|
|
|
|
if (! (new Google2FA)->verifyKey($this->secret, preg_replace('/\s+/', '', $this->code))) {
|
|
throw ValidationException::withMessages(['code' => 'Code stimmt nicht — prüfe die Uhrzeit der Authenticator-App.']);
|
|
}
|
|
|
|
Auth::user()->forceFill([
|
|
'two_factor_secret' => $this->secret,
|
|
'two_factor_confirmed_at' => now(),
|
|
])->save();
|
|
|
|
return $this->redirect(route('dashboard'), navigate: true);
|
|
}
|
|
|
|
public function qrCode(): string
|
|
{
|
|
return (new Google2FA)->getQRCodeInline('Clusev', Auth::user()->email, $this->secret);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.auth.two-factor-setup');
|
|
}
|
|
}
|