Logging in with either username or email.
This commit is contained in:
parent
0fed56e37a
commit
45f6074c9c
|
|
@ -8,6 +8,7 @@ use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\RateLimiter;
|
use Illuminate\Support\Facades\RateLimiter;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
|
use App\Models\User;
|
||||||
|
|
||||||
class LoginRequest extends FormRequest
|
class LoginRequest extends FormRequest
|
||||||
{
|
{
|
||||||
|
|
@ -41,7 +42,7 @@ class LoginRequest extends FormRequest
|
||||||
public function rules()
|
public function rules()
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'username' => ['required', 'string', 'exists:users,username'],
|
'username' => ['required', 'string'],//, 'exists:users,username'],
|
||||||
'password' => ['required', 'string'],
|
'password' => ['required', 'string'],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
@ -57,7 +58,21 @@ class LoginRequest extends FormRequest
|
||||||
{
|
{
|
||||||
$this->ensureIsNotRateLimited();
|
$this->ensureIsNotRateLimited();
|
||||||
|
|
||||||
if (! Auth::attempt($this->only('username', 'password'), $this->boolean('remember'))) {
|
$login_type = filter_var($this->input('username'), FILTER_VALIDATE_EMAIL)
|
||||||
|
? 'email'
|
||||||
|
: 'username';
|
||||||
|
|
||||||
|
$this->merge([
|
||||||
|
$login_type => $this->input('username')
|
||||||
|
]);
|
||||||
|
|
||||||
|
if(!User::where($login_type, $this->only($login_type))->exists()) {
|
||||||
|
throw ValidationException::withMessages([
|
||||||
|
'username' => $this->messages()['username.exists'],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!Auth::attempt($this->only($login_type, 'password'), $this->boolean('remember'))) {
|
||||||
RateLimiter::hit($this->throttleKey());
|
RateLimiter::hit($this->throttleKey());
|
||||||
|
|
||||||
throw ValidationException::withMessages([
|
throw ValidationException::withMessages([
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue