Laravel Fortify
はじめに
Laravel Fortify は、Laravel向けのフロントエンドに依存しない認証バックエンドの実装です。Fortifyは、ログイン、登録、パスワードリセット、メール認証など、Laravelのすべての認証機能を実装するために必要なルートとコントローラを登録します。Fortifyをインストールした後は、route:list
Artisanコマンドを実行して、Fortifyが登録したルートを確認できます。
Fortifyは独自のユーザーインターフェースを提供していないため、登録したルートにリクエストを行うために、独自のユーザーインターフェースとペアにすることが意図されています。このドキュメントの残りの部分で、これらのルートにリクエストを行う方法について詳しく説明します。
フォーティファイは、Laravelの認証機能を実装する際のヘッドスタートを提供するためのパッケージです。使用は必須ではありません。 常に、authentication、password reset、およびemail verificationのドキュメントに記載されている手順に従って、Laravelの認証サービスと手動でやり取りすることができます。
フォーティファイとは?
前述の通り、Laravel Fortifyは、Laravel向けのフロントエンドに依存しない認証バックエンドの実装です。Fortifyは、ログイン、登録、パスワードリセット、メール認証など、Laravelのすべての認証機能を実装するために必要なルートとコントローラを登録します。
Laravelの認証機能を使用するためには、Fortifyを使用する必要はありません。 常に、authentication、password reset、およびemail verificationのドキュメントに記載されている手順に従って、Laravelの認証サービスと手動でやり取りすることができます。
Laravelに初めて触れる場合は、Laravel Breezeアプリケーションスターターキットを試してみると良いでしょう。Laravel Breezeは、Tailwind CSSで構築されたユーザーインターフェースを含む、アプリケーションの認証スキャフォールディングを提供します。Fortifyとは異なり、Breezeはルートとコントローラを直接アプリケーションに公開します。これにより、Laravelの認証機能を学習し、慣れることができます。その後、Laravel Fortifyにこれらの機能の実装を任せる前に、Laravel Breezeを使用してください。
Laravel Fortifyは基本的に、Laravel Breezeのルートとコントローラを取り込んで、ユーザーインターフェースを含まないパッケージとして提供します。これにより、特定のフロントエンドの意見に縛られることなく、アプリケーションの認証レイヤーのバックエンド実装を迅速にスキャフォールディングすることができます。
フォーティファイを使用すべきタイミングは?
おそらく、いつLaravel Fortifyを使用すべきか疑問に思うかもしれません。まず、Laravelのアプリケーションスターターキットのいずれかを使用している場合は、Laravel Fortifyをインストールする必要はありません。なぜなら、すべてのLaravelのアプリケーションスターターキットはすでに完全な認証実装を提供しているからです。
アプリケーションスターターキットを使用しておらず、アプリケーションが認証機能を必要とする場合、2つのオプションがあります。アプリケーションの認 証機能を手動で実装するか、これらの機能のバックエンド実装としてLaravel Fortifyを使用するかです。
Fortifyをインストールすることを選択すると、ユーザーインターフェースは、ユーザーの認証と登録を行うためにこのドキュメントで詳細に説明されているFortifyの認証ルートにリクエストを行います。
Fortifyの代わりにLaravelの認証サービスと手動でやり取りすることを選択する場合は、認証、パスワードリセット、およびメール検証のドキュメントで利用可能なドキュメントに従うことができます。
Laravel FortifyとLaravel Sanctum
一部の開発者は、Laravel SanctumとLaravel Fortifyの違いについて混乱することがあります。これら2つのパッケージは異なるが関連する問題を解決するため、Laravel FortifyとLaravel Sanctumは相互に排他的ではなく競合するパッケージではありません。
Laravel SanctumはAPIトークンの管理とセッションクッキーまたはトークンを使用して既存のユーザーを認証することに関心があります。Sanctumは、ユーザー登録、パスワードリセットなどを処理するルートを提供しません。
APIを提供するアプリケーションやシングルページアプリケーションのバックエンドとして機能するアプリケーションの認証レイヤーを手動で構築しようと している場合、ユーザー登録、パスワードリセットなどにLaravel Fortifyを利用し、APIトークンの管理やセッション認証にLaravel Sanctumを利用する可能性があります。
インストール
開始するには、Composerパッケージマネージャを使用してFortifyをインストールします。
composer require laravel/fortify
次に、fortify:install
Artisanコマンドを使用してFortifyのリソースを公開します:
php artisan fortify:install
このコマンドは、Fortifyのアクションを app/Actions
ディレクトリに公開します。このディレクトリが存在しない場合は作成されます。さらに、FortifyServiceProvider
、構成ファイル、およびすべての必要なデータベースマイグレーションが公開されます。
次に、データベースをマイグレーションする必要があります:
php artisan migrate
Fortifyの機能
fortify
構成ファイルには features
構成配列が含まれています。この配列は、Fortifyがデフォルトで公開するバックエンドのルート / 機能を定義します。Laravel Jetstream と組み合わせてFortifyを使用していない場合は、ほとんどのLaravelアプリケーションで提供される基本的な認証機能のみを有効にすることをお勧めします:
'features' => [
Features::registration(),
Features::resetPasswords(),
Features::emailVerification(),
],
ビューの無効化
デフォルトでは、Fortifyはビューを返すことを意図したルートを定義します。たとえば、ログイン画面や登録画面です。ただし、JavaScriptで動作するシングルページアプリケーションを構築している場合、これらのルートは不要かもしれません。そのため、アプリケーションの config/fortify.php
構成ファイル内の views
構成値を false
に設定することで、これらのルートを完全に無効にすることができます:
'views' => false,
ビューとパスワードリセットの無効化
Fortifyのビューを無効にすることを選択し、アプリケーションでパスワードリセット機能を実装する場合、引き続きアプリケーションの "パスワードリセット" ビューを表示する責任がある password.reset
という名前のルートを定義する必要があります。これは、Laravelの Illuminate\Auth\Notifications\ResetPassword
通知がパスワードリセットURLを password.reset
名前付きルート経由で生成するためです。
認証
始めるには、Fortifyに「ログイン」ビューを返す方法を指示する必要があります。Fortifyはヘッドレス認証ライブラリです。すでに完了しているLaravelの認証機能のフロントエンド実装をお探しの場合は、アプリケーションスターターキットを使用する必要があり ます。
すべての認証ビューのレンダリングロジックは、Laravel\Fortify\Fortify
クラスを介して利用可能な適切なメソッドを使用してカスタマイズできます。通常、このメソッドをアプリケーションの App\Providers\FortifyServiceProvider
クラスの boot
メソッドから呼び出す必要があります。Fortify は、このビューを返す /login
ルートを定義するのに注意します:
use Laravel\Fortify\Fortify;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Fortify::loginView(function () {
return view('auth.login');
});
// ...
}
ログインテンプレートには、/login
に POST リクエストを行うフォームを含める必要があります。/login
エンドポイントは、email
/ username
と password
の文字列を期待しています。メールアドレス / ユ ーザー名フィールドの名前は、config/fortify.php
構成ファイル内の username
値と一致する必要があります。さらに、Laravel が提供する "remember me" 機能を使用したい場合は、ブール値の remember
フィールドを提供できます。
ログイン試行が成功した場合、Fortify はアプリケーションの fortify
構成ファイル内の home
構成オプションで構成された URI にリダイレクトします。ログインリクエストが XHR リクエストである場合、200 HTTP レスポンスが返されます。
リクエストが失敗した場合、ユーザーはログイン画面にリダイレクトされ、検証エラーは共有される $errors
Blade テンプレート変数 を介して利用可能になります。または、XHR リクエストの場合、検証エラーは 422 HTTP レスポンスで返されます。
ユーザー認証のカスタマイズ
Fortify は、提供された資格情報とアプリケーションに構成された認証ガードに基づいてユーザーを自動的に取得および認証します。ただし、ログイン資格情報の認証方法やユーザーの取得方法を完全にカスタマイズしたい場合があります。幸いなことに、Fortify は Fortify::authenticateUsing
メソッドを使用してこれを簡単に実現できます。
このメソッドは、着信 HTTP リクエストを受け 取るクロージャを受け入れます。クロージャは、リクエストに添付されたログイン資格情報を検証し、関連するユーザーインスタンスを返す責任があります。資格情報が無効であるか、ユーザーが見つからない場合は、クロージャによって null
または false
が返されるべきです。通常、このメソッドは FortifyServiceProvider
の boot
メソッドから呼び出すべきです。
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Fortify;
/**
* Bootstrap any application services.
*/
public function boot(): void
{
Fortify::authenticateUsing(function (Request $request) {
$user = User::where('email', $request->email)->first();
if ($user &&
Hash::check($request->password, $user->password)) {
return $user;
}
});
// ...
}
認証ガード
アプリケーションの fortify
構成ファイル内で Fortify が使用する認証ガードをカスタマイズすることができます。ただし、構成されたガードが Illuminate\Contracts\Auth\StatefulGuard
の実装であることを確認する必要があります。Laravel Fortify を使用して SPA を認証しようとしている場合は、Laravel Sanctum と組み合わせて Laravel のデフォルトの web
ガードを使用する必要があります。
認証パイプラインのカスタマイズ
Laravel Fortify は、呼び出し可能なクラスのパイプラインを介してログインリクエストを認証します。必要に応じて、ログインリクエストをパイプラインを通過するカスタムクラスのパイプラインを定義することができます。各クラスは、受信した Illuminate\Http\Request
インスタンスと、ミドルウェアのように、パイプライン内の次のクラスにリクエストを渡すために呼び出される $next
変数を受け取る __invoke
メソッドを持つ必要があります。
カスタムパイプラインを定義するには、Fortify::authenticateThrough
メソッドを使用できます。このメソッドは、ログインリクエストをパイプラインを通過するクラスの配列を返すクロージャを受け入れます。通常、このメソッドは、App\Providers\FortifyServiceProvider
クラスの boot
メソッドから呼び出すべきです。
以下の例は、独自の変更を行う際の出発点として使用できるデフォルトのパイプライン定義を含んでいます:
use Laravel\Fortify\Actions\AttemptToAuthenticate;
use Laravel\Fortify\Actions\EnsureLoginIsNotThrottled;
use Laravel\Fortify\Actions\PrepareAuthenticatedSession;
use Laravel\Fortify\Actions\RedirectIfTwoFactorAuthenticatable;
use Laravel\Fortify\Fortify;
use Illuminate\Http\Request;
Fortify::authenticateThrough(function (Request $request) {
return array_filter([
config('fortify.limiters.login') ? null : EnsureLoginIsNotThrottled::class,
Features::enabled(Features::twoFactorAuthentication()) ? RedirectIfTwoFactorAuthenticatable::class : null,
AttemptToAuthenticate::class,
PrepareAuthenticatedSession::class,
]);
});
認証のスロットリング
デフォルトでは、Fortify は EnsureLoginIsNotThrottled
ミドルウェアを使用して認証試行をスロットリングします。このミドルウェアは、ユーザ名と IP アドレスの組み合わせに固有の試行をスロットリングします。
一部のアプリケーションでは、IP アドレス単位でのスロットリングなど、異なるアプローチが必要な場合があります。そのため、Fortify では fortify.limiters.login
構成オプションを介して独自の レートリミッター を指定できます。もちろん、この構成オプションは、アプリケーションの config/fortify.php
構成ファイルにあります。
スロットリング、二要素認証、および外部ウェブアプリケーションファイアウォール(WAF)の組み合わせを使用すると、正当なアプリケーションユーザーのための最も堅牢な防御が提供されます。
リダイレクトのカスタマイズ
ログイン試行が成功した場合、Fortify は、アプリケーションの fortify
構成ファイル内の home
構成オプションで構成された URI にリダイレクトします。ログインリクエストが XHR リクエストである場合、200 HTTP レスポンスが返されます。ユーザーがアプリケーションからログアウトすると、ユーザーは /
URI にリダイレクトされます。
この動作を高度にカスタマイズする必要がある場合は、LoginResponse
および LogoutResponse
の実装を Laravel のサービスコンテナにバインドすることができます。通常、これはアプリケーションの App\Providers\FortifyServiceProvider
クラスの register
メソッド内で行う必要があ ります:
use Laravel\Fortify\Contracts\LogoutResponse;
/**
* Register any application services.
*/
public function register(): void
{
$this->app->instance(LogoutResponse::class, new class implements LogoutResponse {
public function toResponse($request)
{
return redirect('/');
}
});
}
二要素認証
Fortify の二要素認証機能が有効になっている場合、ユーザーは認証プロセス中に 6 桁の数字トークンを入力する必要があります。このトークンは、Google Authenticator などの TOTP 互換のモバイル認証アプリケーションから取得できる、時間ベースのワンタイムパスワード(TOTP)を使用して生成されます。
始める前に、アプリケーションの App\Models\User
モデルが Laravel\Fortify\TwoFactorAuthenticatable
トレイトを使用していることを確認してください:
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
class User extends Authenticatable
{
use Notifiable, TwoFactorAuthenticatable;
}
Next, you should build a screen within your application where users can manage their two factor authentication settings. This screen should allow the user to enable and disable two factor authentication, as well as regenerate their two factor authentication recovery codes.
By default, the
features
array of thefortify
configuration file instructs Fortify's two factor authentication settings to require password confirmation before modification. Therefore, your application should implement Fortify's password confirmation feature before continuing.
Enabling Two Factor Authentication
To begin enabling two factor authentication, your application should make a POST request to the /user/two-factor-authentication
endpoint defined by Fortify. If the request is successful, the user will be redirected back to the previous URL and the status
session variable will be set to two-factor-authentication-enabled
. You may detect this status
session variable within your templates to display the appropriate success message. If the request was an XHR request, 200
HTTP response will be returned.
After choosing to enable two factor authentication, the user must still "confirm" their two factor authentication configuration by providing a valid two factor authentication code. So, your "success" message should instruct the user that two factor authentication confirmation is still required:
@if (session('status') == 'two-factor-authentication-enabled')
```php
<div class="mb-4 font-medium text-sm">
Please finish configuring two factor authentication below.
</div>
@endif
Next, you should display the two factor authentication QR code for the user to scan into their authenticator application. If you are using Blade to render your application's frontend, you may retrieve the QR code SVG using the twoFactorQrCodeSvg
method available on the user instance:
$request->user()->twoFactorQrCodeSvg();
If you are building a JavaScript powered frontend, you may make an XHR GET request to the `/user/two-factor-qr-code` endpoint to retrieve the user's two factor authentication QR code. This endpoint will return a JSON object containing an `svg` key.
<a name="confirming-two-factor-authentication"></a>
#### Confirming Two Factor Authentication
In addition to displaying the user's two factor authentication QR code, you should provide a text input where the user can supply a valid authentication code to "confirm" their two factor authentication configuration. This code should be provided to the Laravel application via a POST request to the `/user/confirmed-two-factor-authentication` endpoint defined by Fortify.
If the request is successful, the user will be redirected back to the previous URL and the `status` session variable will be set to `two-factor-authentication-confirmed`:
```html
@if (session('status') == 'two-factor-authentication-confirmed')
```php
<div class="mb-4 font-medium text-sm">
Two factor authentication confirmed and enabled successfully.
</div>
@endif
If the request to the two factor authentication confirmation endpoint was made via an XHR request, a 200
HTTP response will be returned.
Displaying the Recovery Codes
You should also display the user's two factor recovery codes. These recovery codes allow the user to authenticate if they lose access to their mobile device. If you are using Blade to render your application's frontend, you may access the recovery codes via the authenticated user instance:
(array) $request->user()->recoveryCodes()
If you are building a JavaScript powered frontend, you may make an XHR GET request to the `/user/two-factor-recovery-codes` endpoint. This endpoint will return a JSON array containing the user's recovery codes.
To regenerate the user's recovery codes, your application should make a POST request to the `/user/two-factor-recovery-codes` endpoint.
<a name="authenticating-with-two-factor-authentication"></a>
### Authenticating With Two Factor Authentication
During the authentication process, Fortify will automatically redirect the user to your application's two factor authentication challenge screen. However, if your application is making an XHR login request, the JSON response returned after a successful authentication attempt will contain a JSON object that has a `two_factor` boolean property. You should inspect this value to know whether you should redirect to your application's two factor authentication challenge screen.
To begin implementing two factor authentication functionality, we need to instruct Fortify how to return our two factor authentication challenge view. All of Fortify's authentication view rendering logic may be customized using the appropriate methods available via the `Laravel\Fortify\Fortify` class. Typically, you should call this method from the `boot` method of your application's `App\Providers\FortifyServiceProvider` class:
```php
use Laravel\Fortify\Fortify;
```php
/**
* 任意のアプリケーションサービスを起動します。
*/
public function boot(): void
{
```php
Fortify::twoFactorChallengeView(function () {
return view('auth.two-factor-challenge');
});
// ...
}
Fortify will take care of defining the /two-factor-challenge
route that returns this view. Your two-factor-challenge
template should include a form that makes a POST request to the /two-factor-challenge
endpoint. The /two-factor-challenge
action expects a code
field that contains a valid TOTP token or a recovery_code
field that contains one of the user's recovery codes.
If the login attempt is successful, Fortify will redirect the user to the URI configured via the home
configuration option within your application's fortify
configuration file. If the login request was an XHR request, a 204 HTTP response will be returned.
If the request was not successful, the user will be redirected back to the two factor challenge screen and the validation errors will be available to you via the shared $errors
Blade template variable. Or, in the case of an XHR request, the validation errors will be returned with a 422 HTTP response.
Disabling Two Factor Authentication
To disable two factor authentication, your application should make a DELETE request to the /user/two-factor-authentication
endpoint. Remember, Fortify's two factor authentication endpoints require password confirmation prior to being called.
Registration
To begin implementing our application's registration functionality, we need to instruct Fortify how to return our "register" view. Remember, Fortify is a headless authentication library. If you would like a frontend implementation of Laravel's authentication features that are already completed for you, you should use an application starter kit.
All of Fortify's view rendering logic may be customized using the appropriate methods available via the Laravel\Fortify\Fortify
class. Typically, you should call this method from the boot
method of your App\Providers\FortifyServiceProvider
class:
use Laravel\Fortify\Fortify;
/**
* 任意のアプリケーションサービスを起動します。
*/
public function boot(): void
{
```php
Fortify::registerView(function () {
return view('auth.register');
});
// ...
}
Fortify will take care of defining the /register
route that returns this view. Your register
template should include a form that makes a POST request to the /register
endpoint defined by Fortify.
The /register
endpoint expects a string name
, string email address / username, password
, and password_confirmation
fields. The name of the email / username field should match the username
configuration value defined within your application's fortify
configuration file.
If the registration attempt is successful, Fortify will redirect the user to the URI configured via the home
configuration option within your application's fortify
configuration file. If the request was an XHR request, a 201 HTTP response will be returned.
If the request was not successful, the user will be redirected back to the registration screen and the validation errors will be available to you via the shared $errors
Blade template variable. Or, in the case of an XHR request, the validation errors will be returned with a 422 HTTP response.
Customizing Registration
The user validation and creation process may be customized by modifying the App\Actions\Fortify\CreateNewUser
action that was generated when you installed Laravel Fortify.
Password Reset
Requesting a Password Reset Link
To begin implementing our application's password reset functionality, we need to instruct Fortify how to return our "forgot password" view. Remember, Fortify is a headless authentication library. If you would like a frontend implementation of Laravel's authentication features that are already completed for you, you should use an application starter kit.
All of Fortify's view rendering logic may be customized using the appropriate methods available via the Laravel\Fortify\Fortify
class. Typically, you should call this method from the boot
method of your application's App\Providers\FortifyServiceProvider
class:
use Laravel\Fortify\Fortify;
/**
* 任意のアプリケーションサービスを起動します。
*/
public function boot(): void
{
```php
Fortify::requestPasswordResetLinkView(function () {
return view('auth.forgot-password');
});
// ...
}
Fortify will take care of defining the /forgot-password
endpoint that returns this view. Your forgot-password
template should include a form that makes a POST request to the /forgot-password
endpoint.
The /forgot-password
endpoint expects a string email
field. The name of this field / database column should match the email
configuration value within your application's fortify
configuration file.
Handling the Password Reset Link Request Response
If the password reset link request was successful, Fortify will redirect the user back to the /forgot-password
endpoint and send an email to the user with a secure link they can use to reset their password. If the request was an XHR request, a 200 HTTP response will be returned.
After being redirected back to the /forgot-password
endpoint after a successful request, the status
session variable may be used to display the status of the password reset link request attempt.
The value of the $status
session variable will match one of the translation strings defined within your application's passwords
language file. If you would like to customize this value and have not published Laravel's language files, you may do so via the lang:publish
Artisan command:
@if (session('status'))
```php
<div class="mb-4 font-medium text-sm text-green-600">
{{ session('status') }}
</div>
@endif
If the request was not successful, the user will be redirected back to the request password reset link screen and the validation errors will be available to you via the shared $errors
Blade template variable. Or, in the case of an XHR request, the validation errors will be returned with a 422 HTTP response.
Resetting the Password
To finish implementing our application's password reset functionality, we need to instruct Fortify how to return our "reset password" view.
All of Fortify's view rendering logic may be customized using the appropriate methods available via the Laravel\Fortify\Fortify
class. Typically, you should call this method from the boot
method of your application's App\Providers\FortifyServiceProvider
class:
use Laravel\Fortify\Fortify;
use Illuminate\Http\Request;
/**
* 任意のアプリケーションサービスを起動します。
*/
public function boot(): void
{
```php
Fortify::resetPasswordView(function (Request $request) {
return view('auth.reset-password', ['request' => $request]);
});
// ...
}
Fortify will take care of defining the route to display this view. Your reset-password
template should include a form that makes a POST request to /reset-password
.
The /reset-password
endpoint expects a string email
field, a password
field, a password_confirmation
field, and a hidden field named token
that contains the value of request()->route('token')
. The name of the "email" field / database column should match the email
configuration value defined within your application's fortify
configuration file.
Handling the Password Reset Response
If the password reset request was successful, Fortify will redirect back to the /login
route so that the user can log in with their new password. In addition, a status
session variable will be set so that you may display the successful status of the reset on your login screen:
@if (session('status'))
```php
<div class="mb-4 font-medium text-sm text-green-600">
{{ session('status') }}
</div>
@endif
If the request was an XHR request, a 200 HTTP response will be returned.
If the request was not successful, the user will be redirected back to the reset password screen and the validation errors will be available to you via the shared $errors
Blade template variable. Or, in the case of an XHR request, the validation errors will be returned with a 422 HTTP response.
Customizing Password Resets
The password reset process may be customized by modifying the App\Actions\ResetUserPassword
action that was generated when you installed Laravel Fortify.
Email Verification
After registration, you may wish for users to verify their email address before they continue accessing your application. To get started, ensure the emailVerification
feature is enabled in your fortify
configuration file's features
array. Next, you should ensure that your App\Models\User
class implements the Illuminate\Contracts\Auth\MustVerifyEmail
interface.
Once these two setup steps have been completed, newly registered users will receive an email prompting them to verify their email address ownership. However, we need to inform Fortify how to display the email verification screen which informs the user that they need to go click the verification link in the email.
All of Fortify's view's rendering logic may be customized using the appropriate methods available via the Laravel\Fortify\Fortify
class. Typically, you should call this method from the boot
method of your application's App\Providers\FortifyServiceProvider
class:
use Laravel\Fortify\Fortify;
/**
* 任意のアプリケーションサービスを起動します。
*/
public function boot(): void
{
```php
Fortify::verifyEmailView(function () {
return view('auth.verify-email');
});
// ...
}
Fortify will take care of defining the route that displays this view when a user is redirected to the /email/verify
endpoint by Laravel's built-in verified
middleware.
Your verify-email
template should include an informational message instructing the user to click the email verification link that was sent to their email address.
Resending Email Verification Links
If you wish, you may add a button to your application's verify-email
template that triggers a POST request to the /email/verification-notification
endpoint. When this endpoint receives a request, a new verification email link will be emailed to the user, allowing the user to get a new verification link if the previous one was accidentally deleted or lost.
If the request to resend the verification link email was successful, Fortify will redirect the user back to the /email/verify
endpoint with a status
session variable, allowing you to display an informational message to the user informing them the operation was successful. If the request was an XHR request, a 202 HTTP response will be returned:
@if (session('status') == 'verification-link-sent')
```php
<div class="mb-4 font-medium text-sm text-green-600">
A new email verification link has been emailed to you!
</div>
@endif
Protecting Routes
To specify that a route or group of routes requires that the user has verified their email address, you should attach Laravel's built-in verified
middleware to the route. The verified
middleware alias is automatically registered by Laravel and serves as an alias for the Illuminate\Routing\Middleware\ValidateSignature
middleware:
Route::get('/dashboard', function () {
```php
// ...
})->middleware(['verified']);
Password Confirmation
While building your application, you may occasionally have actions that should require the user to confirm their password before the action is performed. Typically, these routes are protected by Laravel's built-in password.confirm
middleware.
To begin implementing password confirmation functionality, we need to instruct Fortify how to return our application's "password confirmation" view. Remember, Fortify is a headless authentication library. If you would like a frontend implementation of Laravel's authentication features that are already completed for you, you should use an application starter kit.
All of Fortify's view rendering logic may be customized using the appropriate methods available via the Laravel\Fortify\Fortify
class. Typically, you should call this method from the boot
method of your application's App\Providers\FortifyServiceProvider
class:
use Laravel\Fortify\Fortify;
/**
* 任意のアプリケーションサービスを起動します。
*/
public function boot(): void
{
```php
Fortify::confirmPasswordView(function () {
return view('auth.confirm-password');
});
// ...
}
Fortifyは`/user/confirm-password`エンドポイントを定義し、このビューを返します。`confirm-password`テンプレートには、ユーザーの現在のパスワードを含む`password`フィールドを含むフォームが必要です。
パスワードがユーザーの現在のパスワードと一致する場合、Fortifyはユーザーをアクセスしようとしていたルートにリダイレクトします。リクエストがXHRリクエストである場合、201 HTTPレスポンスが返されます。
リクエストが成功しなかった場合、ユーザーは確認パスワード画面にリダイレクトされ、検証エラーは共有された`$errors` Bladeテンプレート変数を介して利用できます。または、XHRリクエストの場合、検証エラーは422 HTTPレスポンスで返されます。
```markdown
# Markdown Syntax Guide
## Introduction
Markdown is a lightweight markup language with plain-text formatting syntax. Its design allows it to be converted to many output formats, but the original tool by the same name only supports HTML.
## Basic Syntax
These are the elements outlined in John Gruber's original design document. All Markdown applications support these elements.
## Heading
# H1
## H2
### H3
#### H4
##### H5
###### H6