LoginSignup
2
5

More than 3 years have passed since last update.

【Laravel5.8】メール確認リンクとパスワードリセットリンクの有効期限を変更する

Last updated at Posted at 2019-09-04

メール認証リンクとパスワードリセットリンクの有効期限を変更する

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以下に設定箇所があります。
ここのユーザタイプごと(デフォルトでadminusers)の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,
        ],
    ],

おわり

  • セキュリティの事考えるとあまり長い時間に変更するのは避けたほうがいいかもしれません

参考

2
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5