3 namespace App\Http\Requests\Auth;
5 use Illuminate\Auth\Events\Lockout;
6 use Illuminate\Foundation\Http\FormRequest;
7 use Illuminate\Support\Facades\Auth;
8 use Illuminate\Support\Facades\RateLimiter;
9 use Illuminate\Support\Str;
10 use Illuminate\Validation\ValidationException;
12 class LoginRequest extends FormRequest
15 * Determine if the user is authorized to make this request.
19 public function authorize()
25 * Get the validation rules that apply to the request.
29 public function rules()
32 'email' => ['required', 'string', 'email'],
33 'password' => ['required', 'string'],
38 * Attempt to authenticate the request's credentials.
42 * @throws \Illuminate\Validation\ValidationException
44 public function authenticate()
46 $this->ensureIsNotRateLimited();
48 if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
49 RateLimiter::hit($this->throttleKey());
51 throw ValidationException::withMessages([
52 'email' => trans('auth.failed'),
56 RateLimiter::clear($this->throttleKey());
60 * Ensure the login request is not rate limited.
64 * @throws \Illuminate\Validation\ValidationException
66 public function ensureIsNotRateLimited()
68 if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
72 event(new Lockout($this));
74 $seconds = RateLimiter::availableIn($this->throttleKey());
76 throw ValidationException::withMessages([
77 'email' => trans('auth.throttle', [
78 'seconds' => $seconds,
79 'minutes' => ceil($seconds / 60),
85 * Get the rate limiting throttle key for the request.
89 public function throttleKey()
91 return Str::lower($this->input('email')).'|'.$this->ip();