15. Laravel Fortify Authentication - mzm-dev/laravel-latihan GitHub Wiki

Installation

  1. To get started, install Fortify using the Composer package manager:
composer require laravel/fortify
  1. Next, publish Fortify's resources using the vendor:publish command:
php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
  1. Next, you should migrate your database:
php artisan migrate
  1. You should ensure this class is registered within the providers array of your application's config/app.php configuration file.
<?php

return [

    'providers' => [

        // ...........
        // ...........
        /*
         * Package Service Providers...
         */
        App\Providers\FortifyServiceProvider::class,
        // ...........
        // ...........
    ],

Fortify Features

The fortify configuration file contains a features configuration array config/fortify.php configuration file.

    'features' => [
        Features::registration(),
        Features::resetPasswords(),
        //Features::emailVerification(),
        Features::updateProfileInformation(),
        Features::updatePasswords(),
        //Features::twoFactorAuthentication([
        //    'confirmPassword' => false,
        //]),
    ],

Authentication

To get started, we need to instruct Fortify how to return our view in App\Providers\FortifyServiceProvider.php

All of the authentication view's rendering logic may be customized using the appropriate methods available via the Laravel\Fortify\Fortify class.

// ..............
// ..............
  /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Fortify::loginView(function () {
            return view('auth.login');
        });

        Fortify::authenticateUsing(function (Request $request) {
            $user = User::where('email', $request->email)->first();

            if (
                $user &&
                Hash::check($request->password, $user->password)
            ) {
                return $user;
            }
        });

        Fortify::registerView(function () {
            return view('auth.register');
        });

        Fortify::requestPasswordResetLinkView(function () {
            return view('auth.passwords.email');
        });

        Fortify::resetPasswordView(function ($request) {
            return view('auth.passwords.reset', ['request' => $request]);
        });

        Fortify::verifyEmailView(function () {
            return view('auth.verify');
        });
// ..............
// ..............
    }

Add Auth Views

In your resources/views folder create folders

../resources
..../views
....../layouts
....../auth
....../auth/passwords

auth , auth/passwords and layouts

Next, create the following views

resources/views/layouts/app.blade.php

resources/views/auth/login.blade.php

resources/views/auth/register.blade.php

resources/views/auth/verify.blade.php

resources/views/auth/passwords/confirm.blade.php

resources/views/auth/passwords/email.blade.php

resources/views/auth/passwords/reset.blade.php

Code details in (https://github.com/mzm-dev/laravel-latihan/tree/main/direktori/resources/views)

Implements MustVerifyEmail

In your app/Models/User.php file ensure the class implements MustVerifyEmail

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;

    // ...
}

We also need to tell fortify that we want to enable email verification. In the app/fortify.php file uncomment the line that says Features::emailVerification()

If you want to test email verification you will need to update your email variables in .env.

You can use a free mail server smtp debugmail.io

MAIL_MAILER=smtp
MAIL_HOST=debugmail.io
MAIL_PORT=25
MAIL_USERNAME=YOUR_USERNAME_HERE
MAIL_PASSWORD=YOUR_PASSWORD_HERE
MAIL_ENCRYPTION=null
[email protected]
MAIL_FROM_NAME="${APP_NAME}"

Clearing Configuration Cache

Every yout make changer on configuration file please run

$ php artisan config:cache

Configuration cache cleared!
Configuration cached successfully!