LoginRequest::authenticate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
namespace App\Http\Requests\Auth;
4
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;
11
12
class LoginRequest extends FormRequest
13
{
14
    /**
15
     * Determine if the user is authorized to make this request.
16
     *
17
     * @return bool
18
     */
19
    public function authorize()
20
    {
21
        return true;
22
    }
23
24
    /**
25
     * Get the validation rules that apply to the request.
26
     *
27
     * @return array
28
     */
29
    public function rules()
30
    {
31
        return [
32
            'email' => ['required', 'string', 'email'],
33
            'password' => ['required', 'string'],
34
        ];
35
    }
36
37
    /**
38
     * Attempt to authenticate the request's credentials.
39
     *
40
     * @return void
41
     *
42
     * @throws \Illuminate\Validation\ValidationException
43
     */
44
    public function authenticate()
45
    {
46
        $this->ensureIsNotRateLimited();
47
48
        if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
49
            RateLimiter::hit($this->throttleKey());
50
51
            throw ValidationException::withMessages([
52
                'email' => __('auth.failed'),
53
            ]);
54
        }
55
56
        RateLimiter::clear($this->throttleKey());
57
    }
58
59
    /**
60
     * Ensure the login request is not rate limited.
61
     *
62
     * @return void
63
     *
64
     * @throws \Illuminate\Validation\ValidationException
65
     */
66
    public function ensureIsNotRateLimited()
67
    {
68
        if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
69
            return;
70
        }
71
72
        event(new Lockout($this));
73
74
        $seconds = RateLimiter::availableIn($this->throttleKey());
75
76
        throw ValidationException::withMessages([
77
            'email' => trans('auth.throttle', [
78
                'seconds' => $seconds,
79
                'minutes' => ceil($seconds / 60),
80
            ]),
81
        ]);
82
    }
83
84
    /**
85
     * Get the rate limiting throttle key for the request.
86
     *
87
     * @return string
88
     */
89
    public function throttleKey()
90
    {
91
        return Str::lower($this->input('email')).'|'.$this->ip();
92
    }
93
}
94