はじめに
Laravel8 JetStreamには会員登録時に認証メールを導入する機能があって便利だけど
設定後に認証メール送信せずにユーザー登録できるルートも用意したかったので備忘録として残します。
結論
trait MustVerifyEmail hasVerifiedEmail()
のnull判定により、認証メールが送信されていたので
新規登録時にemail_verified_at
を挿入できるように fillable
に追加する
1. ユーザー登録時に email_verified_at
に時刻を挿入する
use Carbon\Carbon;
$user = User::create([
'email' => $request->input('email'),
'name' => $request->input('name'),
'email_verified_at' => Carbon::now() //時刻設定して認証メールをスキップする
]);
2. Userモデルのfillable
に email_verified_at
を追加する
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
/** 省略 */
use App\Models\SnsProvider;
class User extends Authenticatable implements MustVerifyEmail
{
use HasApiTokens,HasFactory,HasProfilePhoto,Notifiable,SoftDeletes;
/**
* The attributes that are mass assignable.
*
* @var string[]
*/
protected $fillable = [
'name',
'email',
'password',
'zip',
'prefecture',
'address',
'email_verified_at' //登録時に追加できるようにするため
];
これで登録時に認証メールが送信されないようになる。
あとは、Auth::login('$user')
などでログインすればオッケー。
ちなみに
MustVerifyEmail のhasVerifiedEmail()
はlaravel内で条件分岐として使用されている。
notify()を使用してメール送信処理も行われている。
namespace Illuminate\Auth;
use Illuminate\Auth\Notifications\VerifyEmail;
trait MustVerifyEmail
{
/**
* Determine if the user has verified their email address.
*
* @return bool
*/
public function hasVerifiedEmail()
{
return ! is_null($this->email_verified_at);
}
間違いあればコメントください。