65 lines
1.8 KiB
PHP
65 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Auth;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\Validate;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.auth')]
|
|
class Login extends Component
|
|
{
|
|
#[Validate('required|email')]
|
|
public string $email = '';
|
|
|
|
#[Validate('required')]
|
|
public string $password = '';
|
|
|
|
public bool $remember = false;
|
|
|
|
public function authenticate()
|
|
{
|
|
$this->validate();
|
|
|
|
$key = 'login:'.md5(strtolower($this->email).'|'.request()->ip());
|
|
|
|
if (RateLimiter::tooManyAttempts($key, 5)) {
|
|
throw ValidationException::withMessages([
|
|
'email' => __('auth.too_many_attempts', ['seconds' => RateLimiter::availableIn($key)]),
|
|
]);
|
|
}
|
|
|
|
$user = User::where('email', $this->email)->first();
|
|
|
|
if (! $user || ! Hash::check($this->password, $user->password)) {
|
|
RateLimiter::hit($key, 60);
|
|
throw ValidationException::withMessages(['email' => __('auth.invalid_credentials')]);
|
|
}
|
|
|
|
RateLimiter::clear($key);
|
|
|
|
// 2FA gate: hold the login until the TOTP code is verified.
|
|
if ($user->hasTwoFactorEnabled()) {
|
|
session()->put('2fa.user', $user->id);
|
|
session()->put('2fa.remember', $this->remember);
|
|
|
|
return $this->redirect(route('two-factor.challenge'), navigate: true);
|
|
}
|
|
|
|
Auth::login($user, $this->remember);
|
|
session()->regenerate();
|
|
|
|
return $this->redirectIntended(route('dashboard'), navigate: true);
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.auth.login')->title(__('auth.title_login'));
|
|
}
|
|
}
|