Virus Corona (Covid-19)

Dapatkan Informasi terbaru

Contoh Login dengan Username atau Email di Laravel Jetstream

Siti Nurmawanti 07 Apr 2021 33

Artikel ini akan memberikan contoh login laravel jetstream dengan username. Artikel ini menjelaskan secara rinci tentang login dengan nama pengguna dan kata sandi di laravel jetstream. Pada artikel ini, kami akan menerapkan login laravel jetstream dengan username atau email. saya ingin berbagi dengan Anda login laravel jetstream dengan contoh username.

Saya akan memberi Anda langkah demi langkah menambahkan login dengan email atau username di laravel jetstream. Anda dapat dengan mudah mengintegrasikan dengan versi laravel 6, laravel 7 dan laravel 8.

 

setelah berhasil memasang laravel jetstream. Anda perlu melakukan perubahan berikut pada file itu:

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 
use Laravel\Fortify\Fortify;
use Laravel\Fortify\Http\Requests\LoginRequest;
use App\Models\User;
use 
  
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;
            }
        });
    }
}

config/fortify.php

'username' => 'email',
  
to
  
'username' => 'identity',

resources/views/auth/login.blade.php

<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>

Sekarang Anda dapat menjalankan dan memeriksanya.

 

itu akan bekerja seperti di bawah ini:

Saya harap ini dapat membantu Anda...

Tags

laravel login jetstream with username or email laravel jetstream

Related Stories


Latest Stories