メール認証リンクとパスワードリセットリンクの有効期限を変更する
Laravelの機能である「メール確認」やら「パスワードリセット」から送信される
「入力されたメールアドレス宛にメール送信したからそこに書いてあるURLから本登録or再登録してね」
などのメールに記載されるURLにはトークン有効期限が設定されている(デフォルトでは各1時間)ので、その有効期限を変更していきます。
メール確認リンクの有効期限を変更
Notifications\VerifyEmail.php
/**
* Get the verification URL for the given notifiable.
*
* @param mixed $notifiable
* @return string
*/
protected function verificationUrl($notifiable)
{
return URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(60),
['id' => $notifiable->getKey()]
);
}
のCarbon::now()->addMinutes(60)
部分で時間(分)を設定しているので、ここを任意の数値に変更すれば伸ばしたりできます。
実装方法としては\Illuminate\Auth\Notifications\VerifyEmail
クラスを継承してカスタムクラスを作成し、そこでverificationUrl
メソッドをオーバーライドして変更する感じかなと。
以下に手順を書いておきます。
実装例
カスタムクラスを作成
以下のコマンドでNotifications
ディレクトリ以下にVerifyEmailCustom
クラスを生成
php artisan make:notification VerifyEmailCustom
verificationUrl
メソッドをオーバーライドして任意の時間を設定する
app\Notifications\VerifyEmailCustom.php
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Facades\Lang;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
class VerifyEmailCustom extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct()
{
//
}
// ~~~略~~~
/**
* Get the verification URL for the given notifiable.
*
* @param mixed $notifiable
* @return string
*/
protected function verificationUrl($notifiable)
{
return URL::temporarySignedRoute(
'verification.verify',
Carbon::now()->addMinutes(120), // 120分(2時間)へ変更
['id' => $notifiable->getKey()]
);
}
// ~~~略~~~
}
パスワードリセットリンクの有効期限を変更
config\auth.php
以下に設定箇所があります。
ここのユーザタイプごと(デフォルトでadmin
とusers
)のexpire
の値が有効期限(分)なのでここを任意の数値に変更すれば伸ばしたりできます。
config\auth.php
/*
|--------------------------------------------------------------------------
| Resetting Passwords
|--------------------------------------------------------------------------
|
| You may specify multiple password reset configurations if you have more
| than one user table or model in the application and you want to have
| separate password reset settings based on the specific user types.
|
| The expire time is the number of minutes that the reset token should be
| considered valid. This security feature keeps tokens short-lived so
| they have less time to be guessed. You may change this as needed.
|
*/
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 30, // 30分に変更
],
'admins' => [
'provider' => 'admins',
'table' => 'password_resets',
'expire' => 60,
],
],
おわり
- セキュリティの事考えるとあまり長い時間に変更するのは避けたほうがいいかもしれません