fix: login with username or email added.

This commit is contained in:
Paul Couture 2023-12-21 05:16:15 +00:00
parent a156a44130
commit ca34ab4b37
3 changed files with 50 additions and 4 deletions

View File

@ -2,12 +2,14 @@
namespace App\Http\Requests\Auth; namespace App\Http\Requests\Auth;
use App\Models\User;
use Illuminate\Auth\Events\Lockout; use Illuminate\Auth\Events\Lockout;
use Illuminate\Foundation\Http\FormRequest; use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth; 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 Illuminate\Support\Facades\Hash;
class LoginRequest extends FormRequest class LoginRequest extends FormRequest
{ {
@ -27,7 +29,7 @@ public function authorize(): bool
public function rules(): array public function rules(): array
{ {
return [ return [
'email' => ['required', 'string', 'email'], 'login' => ['required', 'string'],
'password' => ['required', 'string'], 'password' => ['required', 'string'],
]; ];
} }
@ -41,6 +43,8 @@ public function authenticate(): void
{ {
$this->ensureIsNotRateLimited(); $this->ensureIsNotRateLimited();
/*
if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) { if (! Auth::attempt($this->only('email', 'password'), $this->boolean('remember'))) {
RateLimiter::hit($this->throttleKey()); RateLimiter::hit($this->throttleKey());
@ -48,7 +52,20 @@ public function authenticate(): void
'email' => trans('auth.failed'), 'email' => trans('auth.failed'),
]); ]);
} }
*/
$user = User::where('email', $this->login)
->orWhere('name', $this->login)
->first();
if (!$user || !Hash::check($this->password, $user->password)) {
RateLimiter::hit($this->throttleKey());
throw ValidationException::withMessages([
'login' => __('auth.failed'),
]);
}
Auth::login($user, $this->boolean('remember'));
RateLimiter::clear($this->throttleKey()); RateLimiter::clear($this->throttleKey());
} }

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->unique('name');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropUnique('name');
});
}
};

View File

@ -5,6 +5,7 @@
<div class="mb-6"> <div class="mb-6">
<h2 class="mb-2">Sign in with your existing account</h2> <h2 class="mb-2">Sign in with your existing account</h2>
<p class="normal">Welcome back! Please enter your credentials to sign in.</p> <p class="normal">Welcome back! Please enter your credentials to sign in.</p>
<p class="normal">If you haven't logged in with your existing account since the upgrade, you need to reset your password using the forgot password link below.</p>
</div> </div>
</div> </div>
</div> </div>
@ -15,9 +16,9 @@
@csrf @csrf
{{-- Email Address --}} {{-- Email Address --}}
<div> <div>
<x-input-label for="email" :value="__('Email')" /> <x-input-label for="login" :value="__('Email or Username')" />
<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 --}}
<div class="mt-4"> <div class="mt-4">