動作環境:Laravel 8
インストール方法
インストールがまだならfortifyをコンポーザーでインストールしましょう。
$ composer require laravel/fortify
必要なファイルを書き出します。
$ php artisan vendor:publish --provider="Laravel\Fortify\FortifyServiceProvider"
2段階認証機能を利用するにはカラムを追加する必要があります。マイグレーションを実行しましょう。
$ php artisan migrate
Routesの変更
デフォルトでは/logoutや/registerのようなルートからのURLになります。
たとえば管理画面の場合/admin/logoutや/admin/registerとかサブディレクトリでアクセスしたいですよね。
その場合は、config/fortify.phpにpathを追加します。
config/fortify.php
<?php use App\Providers\RouteServiceProvider; use Laravel\Fortify\Features; return [ // ... 'path' => 'admin' ];
homeも変更しておくとログイン後のリダイレクト先変更することができます。
'home' => '/admin',
必要な機能だけ使う
Fortifyはさまざまな機能が含まれています。
使用しない機能があったらfeaturesの項目を削除しましょう。
config/fortify.php
'features' => [ Features::registration(), // 登録機能 Features::resetPasswords(), // パスワードリセット Features::emailVerification(), // メール認証 Features::updateProfileInformation(), // 登録情報の更新 Features::updatePasswords(), // パスワードの更新 Features::twoFactorAuthentication([ // 2段階認証 'confirmPassword' => true, ]), ],
ログイン時使用する情報をメールアドレスから変更する
初期設定ではログイン時に使用するカラムはemailとpasswordですが、usernameを変更することで他のカラムに変更することができます。
config/fortify.php
'username' => 'name',
ログイン実施回数の制限
limitersを変更することでログインを試せる回数を制限することができます。
config/fortify.php
'limiters' => [ 'login' => 3, ],
ビューファイルのディレクトリ変更
ビューファイルを入れるディレクトリはviews直下になりますが、FortifyServiceProvider.php設定することで変更することができます。
たとえばviews/admin/authに配置したい場合は次のようになります。
Providers/FortifyServiceProvider.php
public function boot()
{
// ...
// Viewのディレクトリを変更
Fortify::viewPrefix('admin.auth.');
}
app.phpのprovidersに追加します。
config/app.php
'providers' => [ // ... App\Providers\FortifyServiceProvider::class, ],

