laravel郵箱認證

1.首先在UserModel引入郵箱認證相關功能

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail as MustVerifyEmailContract;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Auth\MustVerifyEmail as MustVerifyEmailTrait;

class User extends Authenticatable implements MustVerifyEmailContract
{
    use Notifiable, MustVerifyEmailTrait;

    protected $fillable = [
        'name', 'email', 'password',
    ];

    protected $hidden = [
        'password', 'remember_token',
    ];

    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

我們修改的User實現了 MustVerifyEmailContract 接口
查看其源碼
vendor/laravel/framework/src/illuminate/Contracts/Auth/MustVerifyEmail

<?php

namespace Illuminate\Contracts\Auth;

interface MustVerifyEmail
{
    /**
     * Determine if the user has verified their email address.
     *驗證用戶郵箱是否認證
     * @return bool
     */
    public function hasVerifiedEmail();

    /**
     * Mark the given user's email as verified.
     *將用戶標記爲已認證
     * @return bool
     */
    public function markEmailAsVerified();

    /**
     * Send the email verification notification.
     *發送郵件認證的消息通知
     * @return void
     */
    public function sendEmailVerificationNotification();

    /**
     * Get the email address that should be used for verification.
     *獲取發送郵件地址
     * @return string
     */
    public function getEmailForVerification();
}

在User內部我們添加了 trait MustVerifyEmailTrait 該trait實現 MustVerifyEmailContract接口

查看其源碼
vendor/laravel/framework/src/illuminate/Auth/MustVerifyEmail.php

<?php

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);
    }

    /**
     * Mark the given user's email as verified.
     *將用戶標記爲已認證
     * @return bool
     */
    public function markEmailAsVerified()
    {
        return $this->forceFill([
            'email_verified_at' => $this->freshTimestamp(),
        ])->save();
    }

    /**
     * Send the email verification notification.
     *發送email認證的消息通知
     * @return void
     */
    public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmail);
    }

    /**
     * Get the email address that should be used for verification.
     *獲取發送郵件地址
     * @return string
     */
    public function getEmailForVerification()
    {
        return $this->email;
    }
}

我們要實現的功能是用戶註冊後發送認證郵件,我們查看laravel自帶的 RegisterController控制器
app\Http\Controllers\Auth/RegisterController

查看其源碼發現加載了 RegistersUsers;

再次找到 RegisterUsers;

vendor/laravel/framework/src/illuminate/Foundation/Auth/RegistersUsers.php

我們主要看 register方法

public function register(Request $request)
    {
        //判斷用戶提交數據
        $this->validator($request->all())->validate();
        //創建用戶的同時觸發註冊成功事件,並傳入用戶
        event(new Registered($user = $this->create($request->all())));
        //登錄用戶
        $this->guard()->login($user);

        return $this->registered($request, $user)
                        ?: redirect($this->redirectPath());
    }

其中event這裏很關鍵,這裏手動觸發了 Registered 事件並傳入了用戶
我們可以在
app/Providers/EventServiceProvider.php 看到註冊的事件監聽器

protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
    ];

該監聽器的邏輯就是SendEmail…類
我們在次找到 SendEmailVerificationNotification類的源碼

vendor/laravel/framework/src/Illuminate/Auth/Listeners/SendEmailVerificationNotification.php

裏面就一個handle方法

<?php

namespace Illuminate\Auth\Listeners;

use Illuminate\Auth\Events\Registered;
use Illuminate\Contracts\Auth\MustVerifyEmail;

class SendEmailVerificationNotification
{
    /**
     * Handle the event.
     *
     * @param  \Illuminate\Auth\Events\Registered  $event
     * @return void
     */
    public function handle(Registered $event)
    {
        if ($event->user instanceof MustVerifyEmail && ! $event->user->hasVerifiedEmail()) {
            $event->user->sendEmailVerificationNotification();
        }
    }
}

該方法 判斷user是否繼承自 MustVerifyEmail 並且用戶郵箱沒有認證
如果兩個條件都滿足則調用發送郵件方法。

源碼翻了個遍,相信你應該理解其原理了,接下來我們測試郵件發送功能

在.env 中我們將 MATL_DRIVER=smtp 修改爲 log這樣郵件會保存到 laravel.log文件中

使用其自帶的用戶註冊邏輯與視圖
通過命令
php artisan ui:auth
執行遷移文件
php artisan migrate

訪問路由
{項目域名}/register

填寫相關信息點擊註冊
在這裏插入圖片描述
在laravel.log中發現郵箱認證郵件
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章