Subversion-Projekte lars-tiefland.webanos.zeldi.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
4 lars 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
    public function authorize(): bool
18
    {
19
        return true;
20
    }
21
 
22
    /**
23
     * Get the validation rules that apply to the request.
24
     *
25
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
26
     */
27
    public function rules(): array
28
    {
29
        return [
30
            'email' => ['required', 'string', 'email'],
31
            'password' => ['required', 'string'],
32
        ];
33
    }
34
 
35
    /**
36
     * Attempt to authenticate the request's credentials.
37
     *
38
     * @throws \Illuminate\Validation\ValidationException
39
     */
40
    public function authenticate(): void
41
    {
42
        $this->ensureIsNotRateLimited();
43
 
44
        $auth = Auth::attempt($this->only('email', 'password'), $this->boolean('remember'));
45
 
46
        if (! $auth) {
47
            RateLimiter::hit($this->throttleKey());
48
 
49
            throw ValidationException::withMessages([
50
                'email' => trans('auth.failed'),
51
            ]);
52
        }
53
 
54
        RateLimiter::clear($this->throttleKey());
55
    }
56
 
57
    /**
58
     * Ensure the login request is not rate limited.
59
     *
60
     * @throws \Illuminate\Validation\ValidationException
61
     */
62
    public function ensureIsNotRateLimited(): void
63
    {
64
        if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
65
            return;
66
        }
67
 
68
        event(new Lockout($this));
69
 
70
        $seconds = RateLimiter::availableIn($this->throttleKey());
71
 
72
        throw ValidationException::withMessages([
73
            'email' => trans('auth.throttle', [
74
                'seconds' => $seconds,
75
                'minutes' => ceil($seconds / 60),
76
            ]),
77
        ]);
78
    }
79
 
80
    /**
81
     * Get the rate limiting throttle key for the request.
82
     */
83
    public function throttleKey(): string
84
    {
85
        return Str::transliterate(Str::lower($this->input('email')).'|'.$this->ip());
86
    }
87
}