Connexion par email ou nom d'utilisateur

Le champ de connexion accepte désormais les deux :
- Si la saisie contient un @, Auth::attempt() cherche par email
- Sinon, cherche par name
Champ renommé login dans le formulaire et la LoginRequest.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-05 20:02:50 +02:00
parent ff615ca0a1
commit f5a7407be0
2 changed files with 13 additions and 23 deletions
+9 -19
View File
@@ -28,25 +28,23 @@ class LoginRequest extends FormRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'email' => ['required', 'string', 'email'], 'login' => ['required', 'string'],
'password' => ['required', 'string'], 'password' => ['required', 'string'],
]; ];
} }
/**
* Attempt to authenticate the request's credentials.
*
* @throws ValidationException
*/
public function authenticate(): void public function authenticate(): void
{ {
$this->ensureIsNotRateLimited(); $this->ensureIsNotRateLimited();
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) { $login = $this->string('login')->toString();
$field = filter_var($login, FILTER_VALIDATE_EMAIL) ? 'email' : 'name';
if (! Auth::attempt([$field => $login, 'password' => $this->string('password')->toString()], $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey()); RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([ throw ValidationException::withMessages([
'email' => trans('auth.failed'), 'login' => trans('auth.failed'),
]); ]);
} }
@@ -55,18 +53,13 @@ class LoginRequest extends FormRequest
RateLimiter::hit($this->throttleKey()); RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([ throw ValidationException::withMessages([
'email' => 'Votre compte est désactivé. Contactez un administrateur.', 'login' => 'Votre compte est désactivé. Contactez un administrateur.',
]); ]);
} }
RateLimiter::clear($this->throttleKey()); RateLimiter::clear($this->throttleKey());
} }
/**
* Ensure the login request is not rate limited.
*
* @throws ValidationException
*/
public function ensureIsNotRateLimited(): void public function ensureIsNotRateLimited(): void
{ {
if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) { if (! RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
@@ -78,18 +71,15 @@ class LoginRequest extends FormRequest
$seconds = RateLimiter::availableIn($this->throttleKey()); $seconds = RateLimiter::availableIn($this->throttleKey());
throw ValidationException::withMessages([ throw ValidationException::withMessages([
'email' => trans('auth.throttle', [ 'login' => trans('auth.throttle', [
'seconds' => $seconds, 'seconds' => $seconds,
'minutes' => ceil($seconds / 60), 'minutes' => ceil($seconds / 60),
]), ]),
]); ]);
} }
/**
* Get the rate limiting throttle key for the request.
*/
public function throttleKey(): string public function throttleKey(): string
{ {
return Str::transliterate(Str::lower($this->string('email')).'|'.$this->ip()); return Str::transliterate(Str::lower($this->string('login')).'|'.$this->ip());
} }
} }
+4 -4
View File
@@ -5,11 +5,11 @@
<form method="POST" action="{{ route('login') }}"> <form method="POST" action="{{ route('login') }}">
@csrf @csrf
<!-- Email Address --> <!-- Email ou nom d'utilisateur -->
<div> <div>
<x-input-label for="email" :value="__('Email')" /> <x-input-label for="login" :value="__('Email ou nom d\'utilisateur')" />
<x-text-input id="email" class="block mt-1 w-full" type="email" name="email" :value="old('email')" required autofocus autocomplete="username" /> <x-text-input id="login" class="block mt-1 w-full" type="text" name="login" :value="old('login')" required autofocus autocomplete="username" />
<x-input-error :messages="$errors->get('email')" class="mt-2" /> <x-input-error :messages="$errors->get('login')" class="mt-2" />
</div> </div>
<!-- Password --> <!-- Password -->