111 lines
2.8 KiB
PHP
111 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Requests\Auth;
|
|
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rules\Password;
|
|
|
|
/**
|
|
* Login Form Request
|
|
*
|
|
* Validates user login credentials with enterprise security standards.
|
|
* Implements rate limiting and security validations.
|
|
*
|
|
* @author David Valera Melendez <david@valera-melendez.de>
|
|
* @since February 2025
|
|
*/
|
|
class LoginRequest extends FormRequest
|
|
{
|
|
/**
|
|
* Determine if the user is authorized to make this request.
|
|
*/
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Get the validation rules that apply to the request.
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
return [
|
|
'email' => [
|
|
'required',
|
|
'string',
|
|
'email:rfc,dns',
|
|
'max:255',
|
|
'exists:users,email'
|
|
],
|
|
'password' => [
|
|
'required',
|
|
'string',
|
|
'min:6',
|
|
'max:255'
|
|
],
|
|
'remember' => [
|
|
'boolean'
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom error messages for validation rules.
|
|
*/
|
|
public function messages(): array
|
|
{
|
|
return [
|
|
'email.required' => 'Email address is required.',
|
|
'email.email' => 'Please enter a valid email address.',
|
|
'email.exists' => 'No account found with this email address.',
|
|
'password.required' => 'Password is required.',
|
|
'password.min' => 'Password must be at least 6 characters long.',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Get custom attributes for validator errors.
|
|
*/
|
|
public function attributes(): array
|
|
{
|
|
return [
|
|
'email' => 'email address',
|
|
'password' => 'password',
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prepare the data for validation.
|
|
*/
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$this->merge([
|
|
'email' => strtolower(trim($this->email)),
|
|
'remember' => $this->boolean('remember'),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Handle failed validation attempts for security monitoring
|
|
*
|
|
* Logs failed login validation attempts with security context
|
|
* including IP address, user agent, and validation errors.
|
|
* Essential for detecting potential security threats.
|
|
*
|
|
* @param \Illuminate\Contracts\Validation\Validator $validator
|
|
* @return void
|
|
* @throws \Illuminate\Validation\ValidationException
|
|
*/
|
|
protected function failedValidation($validator): void
|
|
{
|
|
logger()->warning('Login validation failed', [
|
|
'email' => $this->input('email'),
|
|
'ip' => $this->ip(),
|
|
'user_agent' => $this->userAgent(),
|
|
'errors' => $validator->errors()->toArray(),
|
|
]);
|
|
|
|
parent::failedValidation($validator);
|
|
}
|
|
}
|