0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Laravel 12 + Fortify その3 パスワードリセットやメール認証のカスタマイズ

Posted at

はじめに

前回までに、「カスタム認証」や「Blade UIとの連携」方法を解説しました。
今回は、Laravel 12 + Fortifyで「パスワードリセット」や「メール認証(メールアドレス検証)」のカスタマイズ方法について紹介します。


前提

  • Laravel 12 プロジェクト
  • Fortify導入済み
  • メール送信設定済み( .envMAIL_ 設定)

1. パスワードリセットのカスタマイズ

1-1. パスワードリセットのフロー

Fortifyは標準で以下のルートを提供します:

  • POST /forgot-password : リセットリンク送信
  • POST /reset-password : 新パスワード設定

1-2. リセットメールの内容を変更

app/Notifications/ResetPasswordNotification.php を作成(自動生成も可能)

php artisan make:notification ResetPasswordNotification

生成されたクラスを編集:

public function toMail($notifiable)
{
    $url = url(route('password.reset', [
        'token' => $this->token,
        'email' => $notifiable->getEmailForPasswordReset(),
    ], false));

    return (new MailMessage)
        ->subject('パスワードリセット通知')
        ->line('パスワードリセットのリクエストを受け付けました。')
        ->action('パスワードをリセット', $url)
        ->line('このメールに心当たりがない場合は、何もする必要はありません。');
}

1-3. Userモデルで通知クラスを差し替え

use App\Notifications\ResetPasswordNotification;

public function sendPasswordResetNotification($token)
{
    $this->notify(new ResetPasswordNotification($token));
}

2. メール認証(メールアドレス検証)のカスタマイズ

2-1. メール認証の有効化

config/fortify.php:

'features' => [
    // ...
    Features::emailVerification(),
],

2-2. メール認証メールのカスタマイズ

app/Notifications/VerifyEmailNotification.php を作成

php artisan make:notification VerifyEmailNotification

編集例:

public function toMail($notifiable)
{
    $verificationUrl = $this->verificationUrl($notifiable);

    return (new MailMessage)
        ->subject('メールアドレスの確認')
        ->line('下記のボタンをクリックしてメールアドレスを確認してください。')
        ->action('メールアドレスを確認', $verificationUrl)
        ->line('心当たりがない場合は、このメールを無視してください。');
}

2-3. Userモデルで通知クラスを差し替え

use App\Notifications\VerifyEmailNotification;

public function sendEmailVerificationNotification()
{
    $this->notify(new VerifyEmailNotification());
}

3. Bladeビューのカスタマイズ

resources/views/auth/passwords/email.blade.php
resources/views/auth/passwords/reset.blade.php
resources/views/auth/verify-email.blade.php
などを用意し、UIを自由に編集できます。


4. テスト方法

  • パスワードリセット画面からメールアドレスを送信し、カスタムメールが届くか確認
  • 新規登録後、メール認証が必要か・認証メールがカスタム内容で届くか確認

まとめ

  • Fortifyのパスワードリセット/メール認証は通知クラスの差し替えで柔軟にカスタマイズ可能
  • Bladeビューやリダイレクト先も自由に調整できる

参考

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?