63 lines
1.8 KiB
PHP
63 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Auth;
|
|
|
|
use App\Http\Requests\Auth\LoginRequest;
|
|
use App\Services\ActivityLogService;
|
|
use App\Support\RoleRedirect;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.guest')]
|
|
class LoginPage extends Component
|
|
{
|
|
public string $email = '';
|
|
|
|
public string $password = '';
|
|
|
|
public bool $remember = false;
|
|
|
|
public bool $showPassword = false;
|
|
|
|
public function login(ActivityLogService $activityLogService)
|
|
{
|
|
$this->validate((new LoginRequest())->rules());
|
|
|
|
$key = strtolower($this->email).'|'.request()->ip();
|
|
|
|
if (RateLimiter::tooManyAttempts($key, 5)) {
|
|
throw ValidationException::withMessages([
|
|
'email' => 'Слишком много попыток входа. Попробуйте позже.',
|
|
]);
|
|
}
|
|
|
|
if (!Auth::attempt(['email' => $this->email, 'password' => $this->password], $this->remember)) {
|
|
RateLimiter::hit($key, 60);
|
|
|
|
throw ValidationException::withMessages([
|
|
'email' => 'Неверный email или пароль.',
|
|
]);
|
|
}
|
|
|
|
RateLimiter::clear($key);
|
|
|
|
request()->session()->regenerate();
|
|
|
|
$user = auth()->user();
|
|
$user->forceFill(['last_login_at' => now()])->save();
|
|
|
|
$activityLogService->log($user, $user, $user->id, 'login', 'Вход в систему', request()->ip());
|
|
|
|
return redirect()->intended(route(RoleRedirect::routeFor($user)));
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.auth.login-page')
|
|
->title('Вход в систему');
|
|
}
|
|
}
|