55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Auth;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Login extends Component
|
|
{
|
|
public string $email = '';
|
|
public string $password = '';
|
|
public bool $remember = false;
|
|
|
|
protected function rules(): array
|
|
{
|
|
return [
|
|
'email' => 'required|email',
|
|
'password' => 'required|min:6',
|
|
];
|
|
}
|
|
|
|
public function login()
|
|
{
|
|
$this->validate();
|
|
|
|
$throttleKey = Str::transliterate(Str::lower($this->email) . '|' . request()->ip());
|
|
|
|
if (RateLimiter::tooManyAttempts($throttleKey, 5)) {
|
|
$seconds = RateLimiter::availableIn($throttleKey);
|
|
$this->addError('email', "Troppi tentativi. Riprova tra {$seconds} secondi.");
|
|
return;
|
|
}
|
|
|
|
if (!Auth::attempt(['email' => $this->email, 'password' => $this->password], $this->remember)) {
|
|
RateLimiter::hit($throttleKey);
|
|
$this->addError('email', 'Credenziali non valide.');
|
|
return;
|
|
}
|
|
|
|
RateLimiter::clear($throttleKey);
|
|
session()->regenerate();
|
|
activity()->causedBy(auth()->user())->log('login');
|
|
|
|
return redirect()->intended(route('dashboard'));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.auth.login')
|
|
->layout('components.layouts.guest');
|
|
}
|
|
}
|