Laravel Jetstream Auth: Login With Email Or Username Example
In this post, I will show you how to login with email or username with laravel jetstream authentication.
Sometimes, we may need to let users log in using either their email or username. How can you do this this if you've implemented the Laravel jetstream authentication scaffold?
I'll walk you through a simple solution that allows users to log in with either their email or username.
we simply go to FortifyServiceProvider.php provider file and overwrite authenticateUsing() method with the following code:
App/Providers/FortifyServiceProvider.php
<?php
namespace App\Providers;
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\ResetUserPassword;
use App\Actions\Fortify\UpdateUserPassword;
use App\Actions\Fortify\UpdateUserProfileInformation;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use Laravel\Fortify\Fortify;
use App\Models\User;
use Hash;
class FortifyServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*/
public function register(): void
{
//
}
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Fortify::createUsersUsing(CreateNewUser::class);
Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class);
Fortify::updateUserPasswordsUsing(UpdateUserPassword::class);
Fortify::resetUserPasswordsUsing(ResetUserPassword::class);
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;
}
});
RateLimiter::for('login', function (Request $request) {
$throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
return Limit::perMinute(5)->by($throttleKey);
});
RateLimiter::for('two-factor', function (Request $request) {
return Limit::perMinute(5)->by($request->session()->get('login.id'));
});
}
}
You can also see the following video:
You can see the output:
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 11 Custom User Login and Registration Tutorial
- Laravel 11 Google Recaptcha V3 Validation Tutorial
- Laravel 11 Model Events Example Tutorial
- Laravel 11 JQuery UI Ajax Autocomplete Search Example
- Laravel 11 Cron Job Task Scheduling Tutorial
- Laravel 11 Clear Cache of Route, View, Config, Event Commands
- Laravel 11 Send Email using Queue Example
- Laravel 11 Guzzle HTTP Request Example
- Laravel 11 Yajra Datatables Example Tutorial
- Laravel 11 REST API Authentication using Sanctum Tutorial
- Laravel 11 Ajax Request Example Tutorial
- Laravel 11 Markdown | Laravel 11 Send Email using Markdown Mailables
- Laravel 11 Create Custom Helper Functions Example