ItSolutionStuff.com

Laravel Jetstream Auth: Login With Email Or Username Example

By Hardik Savani • October 25, 2024
Laravel

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:

Output:

You can see the output:

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Laravel 11 Custom User Login and Registration Tutorial

Read Now →

Laravel 11 Google Recaptcha V3 Validation Tutorial

Read Now →

Laravel 11 Model Events Example Tutorial

Read Now →

Laravel 11 JQuery UI Ajax Autocomplete Search Example

Read Now →

Laravel 11 Cron Job Task Scheduling Tutorial

Read Now →

Laravel 11 Clear Cache of Route, View, Config, Event Commands

Read Now →

Laravel 11 Send Email using Queue Example

Read Now →

Laravel 11 Guzzle HTTP Request Example

Read Now →

Laravel 11 Yajra Datatables Example Tutorial

Read Now →

Laravel 11 REST API Authentication using Sanctum Tutorial

Read Now →

Laravel 11 Ajax Request Example Tutorial

Read Now →

Laravel 11 Markdown | Laravel 11 Send Email using Markdown Mailables

Read Now →

Laravel 11 Create Custom Helper Functions Example

Read Now →