In this tutorial you will learn about the Laravel Jetstream Customize Login with Username or Email Tutorial and its application with practical example.
In this Laravel Jetstream Customize Login with Username or Email Tutorial I will show how to Customize Login with Username or Email in laravel. In this laravel 8 jetstream Customize Login with Username or Email tutorial I will show you to change login with email to username or login with username to email using jetstream auth in laravel application. Jetstream auth with livewire is laravel package that enable us to generate default laravel authentication scaffolding. Laravel jetstream auth includes fully functional login, register, logout, reset password, forget password, email verification, two-factor authentication, session management. Jetstream auth will include login, register, reset the password, forget password, email verification, and two-factor authentication blade views and controller file.
Laravel Jetstream Customize Login with Username or Email Tutorial
In this step by step tutorial I will guide through on how to Customize Login with Username or Email. You will learn to change login with email to username or login with username to email using jetstream auth in laravel application. Please follow the instruction given below:
- Step 1 – Install Laravel 8 App
- Step 2 – Database Configuration
- Step 3 – Install Auth Scaffolding Jetstream
- Step 4 – Install Livewire Package
- Step 5 – Jetstream Configuration and Customization
- Step 6 – Run PHP artisan Migrate
- Step 7 – Install Npm Packages
- Step 8 – Run Development Server
Step 1 – Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Laravel8Auth |
Step 2 – Database Configuration
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db name DB_USERNAME=db user name DB_PASSWORD=db password |
Step 3 – Install Auth Scaffolding Jetstream
In this step, we will install Jetstream Package via the composer dependency manager. Use the following command to install Jetstream.
1 |
composer require laravel/jetstream |
Step 4 – Install Livewire Package
Now run following command to create basic authentication scaffolding login, register, logout and email verification views file:
1 2 3 4 5 |
php artisan jetstream:install livewire OR php artisan jetstream:install livewire --teams |
Step 5 – Jetstream Configuration and Customization
Next, we will open fortify.php file and will enable and disable option of jetstream package as per requirement, which is located inside config directory.
1 2 3 4 5 6 7 8 |
'features' => [ Features::registration(), Features::resetPasswords(), //Features::emailVerification(), Features::updateProfileInformation(), Features::updatePasswords(), Features::twoFactorAuthentication(), ], |
And replace the following lines of code into fortify.php file:
1 2 3 4 5 |
'username' => 'email', to 'username' => 'identity', |
After installing laravel jetstream successfully. Now, you need to do following changes on that FortifyServiceProvider.php, So visit app/Providers directory and open FortifyServiceProvider.php file then add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?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\Support\ServiceProvider; use Laravel\Fortify\Fortify; use Laravel\Fortify\Http\Requests\LoginRequest; use App\Models\User; use Hash; class FortifyServiceProvider extends ServiceProvider { /** * Register any application services. * * @return void */ public function register() { } /** * Bootstrap any application services. * * @return void */ public function boot() { Fortify::createUsersUsing(CreateNewUser::class); Fortify::updateUserProfileInformationUsing(UpdateUserProfileInformation::class); Fortify::updateUserPasswordsUsing(UpdateUserPassword::class); Fortify::resetUserPasswordsUsing(ResetUserPassword::class); 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; } }); } } |
Then open login.blade.php file, which is placed resources/views/auth/ directory and add the following code into it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
<x-guest-layout> <x-jet-authentication-card> <x-slot name="logo"> <x-jet-authentication-card-logo /> </x-slot> <x-jet-validation-errors class="mb-4" /> @if (session('status')) <div class="mb-4 font-medium text-sm text-green-600"> {{ session('status') }} </div> @endif <form method="POST" action="{{ route('login') }}"> @csrf <div> <x-jet-label value="{{ __('Username/Email') }}" /> <x-jet-input class="block mt-1 w-full" type="text" name="identity" :value="old('email')" required autofocus /> </div> <div class="mt-4"> <x-jet-label value="{{ __('Password') }}" /> <x-jet-input class="block mt-1 w-full" type="password" name="password" required autocomplete="current-password" /> </div> <div class="block mt-4"> <label class="flex items-center"> <input type="checkbox" class="form-checkbox" name="remember"> <span class="ml-2 text-sm text-gray-600">{{ __('Remember me') }}</span> </label> </div> <div class="flex items-center justify-end mt-4"> @if (Route::has('password.request')) <a class="underline text-sm text-gray-600 hover:text-gray-900" href="{{ route('password.request') }}"> {{ __('Forgot your password?') }} </a> @endif <x-jet-button class="ml-4"> {{ __('Login') }} </x-jet-button> </div> </form> </x-jet-authentication-card> </x-guest-layout> |
Step 6 – Run php artisan Migrate
Now, run following command to migrate database schema.
1 |
php artisan migrate |
Step 7 – Install Npm Packages
Now run following command to instal npm dependencies:
1 |
npm install |
Then type the following command on cmd to run npm:
1 |
npm run dev |
Step 8 – Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/ |