Solution working for Laravel-Jetstream
You can authenticate using username or email
following this steps.
1. Confirm login input field name (Let name is identity
)
2. Change config/fortify.php
'username' => 'email' to 'username' => 'identity'
3. Added following authentication code in your app/Providers/FortifyServiceProvider.php
file inside boot method
Fortify::authenticateUsing(function (LoginRequest $request) {
$user = User::where('email', $request->identity)
->orWhere('username', $request->identity)->first();
if (
$user &&
\Hash::check($request->password, $user->password)
) {
return $user;
}
});
[Note] Please use those classes
use Laravel\Fortify\Http\Requests\LoginRequest;
use App\Models\User;
#For register username
1. Add input in your register.blade.php
<div class="mt-4">
<x-jet-label for="username" value="{{ __('User Name') }}" />
<x-jet-input id="username" class="block mt-1 w-full" type="text" name="username" :value="old('username')" required autofocus autocomplete="username" />
</div>
2. Add username
in User model $fillable
array list.
3. Finally change in app/Actions/Fortify/CreateNewUser.php
file
Validator::make($input, [
..........
'username' => ['required', 'string', 'max:255', 'unique:users'],
.........
])->validate();
return User::create([
.......
'username' => $input['username'],
.....
]);
}
Lets enjoy the authentication.