116 lines
3.0 KiB
PHP
116 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Auth;
|
|
|
|
use Illuminate\Auth\Events\Lockout;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\ValidationException;
|
|
use App\Models\User;
|
|
|
|
class LoginRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Izinkan semua user akses request ini
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Aturan validasi input
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'username' => ['required', 'string'],
|
|
'password' => ['required', 'string', 'min:8'],
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Pesan error custom (biar lebih user-friendly)
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'username.required' => 'Username wajib diisi.',
|
|
'username.string' => 'Username harus berupa teks.',
|
|
|
|
'password.required' => 'Password wajib diisi.',
|
|
'password.string' => 'Password harus berupa teks.',
|
|
'password.min' => 'Password minimal 6 karakter.',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Proses autentikasi login
|
|
*/
|
|
public function authenticate(): void
|
|
{
|
|
// Cek limit login (anti brute force)
|
|
$this->ensureIsNotRateLimited();
|
|
|
|
// Jika dua-duanya kosong (optional tambahan biar lebih jelas)
|
|
if (!$this->username && !$this->password) {
|
|
throw ValidationException::withMessages([
|
|
'username' => 'Username dan password wajib diisi.',
|
|
]);
|
|
}
|
|
|
|
// Cek apakah username ada
|
|
$user = User::where('username', $this->username)->first();
|
|
if (!$user) {
|
|
throw ValidationException::withMessages([
|
|
'username' => 'Username tidak terdaftar.',
|
|
]);
|
|
}
|
|
|
|
// Cek password
|
|
if (!Auth::attempt([
|
|
'username' => $this->username,
|
|
'password' => $this->password,
|
|
])) {
|
|
RateLimiter::hit($this->throttleKey());
|
|
|
|
throw ValidationException::withMessages([
|
|
'password' => 'Password yang dimasukkan salah.',
|
|
]);
|
|
}
|
|
|
|
// Reset limit kalau berhasil login
|
|
RateLimiter::clear($this->throttleKey());
|
|
}
|
|
|
|
/**
|
|
* Cek apakah sudah terlalu banyak percobaan login
|
|
*/
|
|
public function ensureIsNotRateLimited(): void
|
|
{
|
|
if (!RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
|
|
return;
|
|
}
|
|
|
|
event(new Lockout($this));
|
|
|
|
$seconds = RateLimiter::availableIn($this->throttleKey());
|
|
|
|
throw ValidationException::withMessages([
|
|
'username' => 'Terlalu banyak percobaan login. Coba lagi dalam ' . $seconds . ' detik.',
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Key unik untuk rate limiter
|
|
*/
|
|
public function throttleKey(): string
|
|
{
|
|
return Str::transliterate(
|
|
Str::lower($this->input('username')) . '|' . $this->ip()
|
|
);
|
|
}
|
|
}
|