feat: enable user authentication via email or username in Fortify

This commit is contained in:
Yoga Pangestu 2026-04-17 22:11:59 +07:00
parent 9af62ff47c
commit 9b1a04c7a2
3 changed files with 19 additions and 7 deletions

View File

@ -4,8 +4,10 @@
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Models\User;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
@ -38,6 +40,16 @@ public function boot(): void
*/
private function configureActions(): void
{
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->login)
->orWhere('username', $request->login)
->first();
if ($user && Hash::check($request->password, $user->password)) {
return $user;
}
});
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
Fortify::createUsersUsing(CreateNewUser::class);
}

View File

@ -45,7 +45,7 @@
|
*/
'username' => 'email',
'username' => 'login',
'email' => 'email',

View File

@ -28,18 +28,18 @@ export default function Login({
<>
<div className="grid gap-6">
<div className="grid gap-2">
<Label htmlFor="email" required>Alamat Surel</Label>
<Label htmlFor="login" required>Alamat Surel atau Nama Pengguna</Label>
<Input
id="email"
type="email"
name="email"
id="login"
type="text"
name="login"
required
autoFocus
tabIndex={1}
autoComplete="off"
placeholder="johndoe@example.com"
placeholder="johndoe@example.com atau johndoe"
/>
<InputError message={errors.email} />
<InputError message={errors.login} />
</div>
<div className="grid gap-2">