イントロダクションIntroduction
">Tip!! 素早く始めたいですか? 真新しくインストールしたLaravelアプリケーションに
laravel/uiComposerパッケージをインストールし、php artisan ui vue --authを実行してください。データベースをマイグレーションし、http://your-app.test/registerかアプリケーションに割り付けた別のURLをブラウザでアクセスしましょう。これらのコマンドがパスワードリセットを含めた、認証システム全体のスカフォールドの面倒見ます。{tip} Want to get started fast? Install thelaravel/uiComposer package and runphp artisan ui vue --authin a fresh Laravel application. After migrating your database, navigate your browser tohttp://your-app.test/registeror any other URL that is assigned to your application. This single command will take care of scaffolding your entire authentication system, including resetting passwords!
大抵のWebアプリケーションはパスワードをリセットする手段を提供しています。それぞれのアプリケーションで何度も実装する代わりに、Laravelはパスワードリマインダを送り、パスワードリセットを実行する便利な方法を提供しています。Most web applications provide a way for users to reset their forgotten passwords. Rather than forcing you to re-implement this on each application, Laravel provides convenient methods for sending password reminders and performing password resets.
Note: Laravelのパスワードリセット機能を使用開始する前に、ユーザーが
Illuminate\Notifications\Notifiableトレイトを使用していることを確認してください。{note} Before using the password reset features of Laravel, your user must use theIlluminate\Notifications\Notifiabletrait.
データベースの検討事項Database Considerations
利用を始めるには、App\UserモデルがIlluminate\Contracts\Auth\CanResetPassword契約を実装しているか確認してください。フレームワークに用意されているApp\Userモデルでは、すでにこのインターフェイスが実装されています。Illuminate\Auth\Passwords\CanResetPasswordトレイトで、このインターフェイスで実装する必要のあるメソッドが定義されています。To get started, verify that your App\User model implements the Illuminate\Contracts\Auth\CanResetPassword contract. The App\User model included with the framework already implements this interface, and uses the Illuminate\Auth\Passwords\CanResetPassword trait to include the methods needed to implement the interface.
リセットトークンテーブルマイグレーションの生成Generating The Reset Token Table Migration
次にパスワードリセットトークンを保存しておくためのテーブルを作成します。このテーブルのマイグレーションはlaravel/ui Composerパッケージに含まれており、パスワードリセットトークン・データベーステーブルを生成するには、migrateコマンドを使用してください。Next, a table must be created to store the password reset tokens. The migration for this table is included in the laravel/ui Composer package. After installing the laravel/ui package, you may use the migrate command to create the password reset token database table:
composer require laravel/ui
php artisan migrate
ルート定義Routing
Laravelはパスワードリセットリンクのメールを送信し、ユーザーのパスワードをリセットするために必要なロジックを全部含んでいる、Auth\ForgotPasswordControllerとAuth\ResetPasswordControllerを用意しています。パスワードリセットに必要な全ルートは、laravel/ui Composerパッケージを使用して生成できます。Laravel includes Auth\ForgotPasswordController and Auth\ResetPasswordController classes that contains the logic necessary to e-mail password reset links and reset user passwords. All of the routes needed to perform password resets may be generated using the laravel/ui Composer package:
composer require laravel/ui
php artisan ui vue --auth
ビューViews
パスワードリセットに必要なすべてのビューは、laravel/ui Composerパッケージを使用して生成できます。To generate all of the necessary view for resetting passwords, you may use the laravel/ui Composer package:
composer require laravel/ui
php artisan ui vue --auth
ビューはresources/views/auth/passwordsの中に設置されます。アプリケーションの必要に合わせ、自由に変更してください。These views are placed in resources/views/auth/passwords. You are free to customize them as needed for your application.
パスワードリセット後の処理After Resetting Passwords
ユーザーのパスワードをリセットするルートとビューを定義できたら、ブラウザーで/password/resetのルートへアクセスできます。フレームワークに含まれている ForgotPasswordControllerは、パスワードリセットリンクを含むメールを送信するロジックを含んでいます。一方のResetPasswordControllerはユーザーパスワードのリセットロジックを含んでいます。Once you have defined the routes and views to reset your user's passwords, you may access the route in your browser at /password/reset. The ForgotPasswordController included with the framework already includes the logic to send the password reset link e-mails, while the ResetPasswordController includes the logic to reset user passwords.
パスワードがリセットされたら、そのユーザーは自動的にアプリケーションにログインされ、/homeへリダイレクトされます。パスワードリセット後のリダイレクト先をカスタマイズするには、ResetPasswordControllerのredirectToプロパティを定義してください。After a password is reset, the user will automatically be logged into the application and redirected to /home. You can customize the post password reset redirect location by defining a redirectTo property on the ResetPasswordController:
protected $redirectTo = '/dashboard';
Note: デフォルトでパスワードリセットトークンは、一時間有効です。これは、
config/auth.phpファイルのexpireオプションにより変更できます。{note} By default, password reset tokens expire after one hour. You may change this via the password resetexpireoption in yourconfig/auth.phpfile.
カスタマイズCustomization
認証ガードのカスタマイズAuthentication Guard Customization
auth.php設定ファイルにより、複数のユーザーテーブルごとに認証の振る舞いを定義するために使用する、「ガード」をそれぞれ設定できます。用意されているResetPasswordControllerコントローラのguardメソッドをオーバーライドすることにより、選択したガードを使用するようにカスタマイズできます。このメソッドは、ガードインスタンスを返す必要があります。In your auth.php configuration file, you may configure multiple "guards", which may be used to define authentication behavior for multiple user tables. You can customize the included ResetPasswordController to use the guard of your choice by overriding the guard method on the controller. This method should return a guard instance:
use Illuminate\Support\Facades\Auth;
/**
* パスワードリセットの間、使用されるガードの取得
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return Auth::guard('guard-name');
}
パスワードブローカーのカスタマイズPassword Broker Customization
複数のユーザーテーブルに対するパスワードをリセットするために使用する、別々のパスワード「ブローカー」をauth.phpファイルで設定できます。用意されているForgotPasswordControllerとResetPasswordControllerのbrokerメソッドをオーバーライドし、選んだブローカーを使用するようにカスタマイズができます。In your auth.php configuration file, you may configure multiple password "brokers", which may be used to reset passwords on multiple user tables. You can customize the included ForgotPasswordController and ResetPasswordController to use the broker of your choice by overriding the broker method:
use Illuminate\Support\Facades\Password;
/**
*パスワードリセットに使われるブローカの取得
*
* @return PasswordBroker
*/
public function broker()
{
return Password::broker('name');
}
リセットメールのカスタマイズReset Email Customization
パスワードリセットリンクをユーザーへ送るために使用する、通知クラスは簡単に変更できます。手始めに、UserモデルのsendPasswordResetNotificationメソッドをオーバーライドしましょう。このメソッドの中で、皆さんが選んだ通知クラスを使用し、通知を送信できます。パスワードリセット$tokenは、メソッドの第1引数として受け取ります。You may easily modify the notification class used to send the password reset link to the user. To get started, override the sendPasswordResetNotification method on your User model. Within this method, you may send the notification using any notification class you choose. The password reset $token is the first argument received by the method:
/**
* パスワードリセット通知の送信
*
* @param string $token
* @return void
*/
public function sendPasswordResetNotification($token)
{
$this->notify(new ResetPasswordNotification($token));
}