clusev/app/Livewire/Auth/TwoFactorSetup.php

58 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\Validate;
use Livewire\Component;
use PragmaRX\Google2FAQRCode\Google2FA;
#[Layout('layouts.auth')]
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' => __('auth.code_mismatch')]);
}
Auth::user()->forceFill([
'two_factor_secret' => $this->secret,
'two_factor_confirmed_at' => now(),
])->save();
// Generate backup codes and show them once before entering the panel.
Auth::user()->replaceRecoveryCodes();
return $this->redirect(route('two-factor.recovery'), 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')->title(__('auth.title_setup'));
}
}