Files
zenithar/app/Livewire/Auth/Login.php

81 lines
2.0 KiB
PHP

<?php
namespace App\Livewire\Auth;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Validate;
use Livewire\Component;
#[Layout('components.layouts.login')]
class Login extends Component
{
#[Validate('required|string|email')]
public $email = '';
#[Validate('required|string')]
public string $password = '';
#[Validate('boolean')]
public bool $remember = false;
public function render(): View
{
return view('livewire.auth.login');
}
public function login(): void
{
Session::invalidate();
$this->authenticate();
Session::regenerate();
$this->redirectIntended(route('home.index'));
}
public function authenticate(): void
{
$this->validate();
$this->ensureIsNotRateLimited();
if (! Auth::attempt($this->only('email', 'password'), $this->remember)) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'email' => __('auth.failed'),
]);
}
RateLimiter::clear($this->throttleKey());
}
protected function ensureIsNotRateLimited(): void
{
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
return;
}
event(new Lockout(request()));
$seconds = RateLimiter::availableIn($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.throttle', [
'seconds' => $seconds,
'minutes' => ceil($seconds / 60),
]),
]);
}
protected function throttleKey(): string
{
return Str::transliterate(Str::lower($this->email).'|'.request()->ip());
}
}