LoginSignup
4
4

More than 3 years have passed since last update.

Laravel Breezeでユーザー登録後にメールアドレスの確認メールが送信されるようにする

Posted at

Laravel Breezeを利用するととても簡単にログイン機能を実装することができます。ただ、ユーザー登録後にメールアドレスの確認メールを送信するには手動で実装する必要があったのでメモ。

前提

下記記事のプロジェクトを利用します。

Laravel SailとLaravel Breezeでさくっとログイン機能を実装する - Qiita
https://qiita.com/kai_kou/items/12c060a8b5e6eb409da8

GitHubリポジトリもありますので、ご参考ください。

実装

app/Models/User.phpUserクラスに対してMustVerifyEmailインターフェースを指定するだけで実現できます。

app/Models/User.php(一部抜粋)
<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

-class User extends Authenticatable
+class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;

裏側でなにをしているのかは下記が詳しくまとめてくれています。(感謝

【Laravel】認証機能の実装方法(Laravel breeze)と処理内容の確認 - Qiita
https://qiita.com/yuta-38/items/360bde6654b6d32dd79e

動作確認

実際にユーザー登録してみるとメール送信されることが確認できました。
image.png
image.png

やったぜ

参考

Email Verification - Laravel - The PHP Framework For Web Artisans
https://laravel.com/docs/8.x/verification#model-preparation

Laravel Breeze - Sending Registration Emails + Email Customizations
https://laracasts.com/discuss/channels/laravel/laravel-breeze-sending-registration-emails-email-customizations

【Laravel】認証機能の実装方法(Laravel breeze)と処理内容の確認 - Qiita
https://qiita.com/yuta-38/items/360bde6654b6d32dd79e

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