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\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use App\Models\User;
|
||||
|
||||
class LoginRequest extends FormRequest
|
||||
{
|
||||
|
|
@ -41,7 +42,7 @@ class LoginRequest extends FormRequest
|
|||
public function rules()
|
||||
{
|
||||
return [
|
||||
'username' => ['required', 'string', 'exists:users,username'],
|
||||
'username' => ['required', 'string'],//, 'exists:users,username'],
|
||||
'password' => ['required', 'string'],
|
||||
];
|
||||
}
|
||||
|
|
@ -56,8 +57,22 @@ class LoginRequest extends FormRequest
|
|||
public function authenticate()
|
||||
{
|
||||
$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());
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
|
|
|
|||
Loading…
Reference in New Issue