josiasmontag/laravel-email-verification

1.2.5 2019-02-28 14:03 UTC

This package is auto-updated.

Last update: 2024-09-04 15:43:55 UTC


README

Build Status Total Downloads Latest Stable Version License

⚠️ 弃用警告:此包已弃用。我建议使用 Laravel 的内置邮箱验证

介绍

该 Laravel 邮箱验证包是为 Laravel 5.4 及更高版本构建的,以便轻松处理用户验证和验证邮箱。它受到了基于加密的密码重置和 jrean 的邮箱验证包的启发。

  • 基于加密的邮箱验证。无需在数据库中存储临时令牌!
  • 基于事件的:无需覆盖您的 register() 方法。
  • 使用 Laravel 5.3 通知系统。
  • 仅允许验证用户访问某些路由,使用 IsEmailVerified 中间件。
  • 允许用户随时重新发送验证邮件。
  • 已准备好本地化。

配置

要开始使用,请使用 Composer 将包添加到项目的依赖项

composer require josiasmontag/laravel-email-verification

在 Laravel 5.5 中,服务提供程序将自动注册。在框架的旧版本中,只需在 config/app.php 配置文件中注册 Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider 即可。

'providers' => [
    // Other service providers...

    Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider::class,
],

迁移

代表用户的表必须更新为具有 verified 列。此更新将由此包包含的迁移执行。

要从此包运行迁移,请使用以下命令

php artisan migrate --path="/vendor/josiasmontag/laravel-email-verification/database/migrations"

此包会尝试通过检查 auth 提供者用户设置中设置的内容来猜测您的 user 表。如果此密钥找不到,将使用默认的 App\User 来获取表名。

要自定义迁移,请使用以下命令发布它

php artisan vendor:publish --provider="Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider" --tag="migrations"

用户模型

代表 User 的模型必须实现 CanVerifyEmail 接口。此包包含一个用于快速实现的 CanVerifyEmail 特性。您可以根据需要自定义此特性以更改激活邮件。

use Illuminate\Foundation\Auth\User as Authenticatable;
use Lunaweb\EmailVerification\Traits\CanVerifyEmail;
use Lunaweb\EmailVerification\Contracts\CanVerifyEmail as CanVerifyEmailContract;

class User extends Authenticatable implements CanVerifyEmailContract
{

    use CanVerifyEmail;

    // ...
}

注册控制器

此包为 RegisterController 提供了一个 VerifiesEmail 特性。您必须更新中间件异常,以允许已认证用户访问 verify 路由。

use Lunaweb\EmailVerification\Traits\VerifiesEmail;

class RegisterController extends Controller
{

    use RegistersUsers, VerifiesEmail;


    public function __construct()
    {
          $this->middleware('guest', ['except' => ['verify', 'showResendVerificationEmailForm', 'resendVerificationEmail']]);
          $this->middleware('auth', ['only' => ['showResendVerificationEmailForm', 'resendVerificationEmail']]);
    }

    // ...

}

无需覆盖 register()。默认情况下,此包监听 Illuminate\Auth\Events\Registered 事件并发送验证邮件。您可以使用 listen_registered_event 设置禁用此行为。

路由

此包添加以下路由。

Route::get('register/verify', 'App\Http\Controllers\Auth\RegisterController@verify')->name('verifyEmailLink');
Route::get('register/verify/resend', 'App\Http\Controllers\Auth\RegisterController@showResendVerificationEmailForm')->name('showResendVerificationEmailForm');
Route::post('register/verify/resend', 'App\Http\Controllers\Auth\RegisterController@resendVerificationEmail')->name('resendVerificationEmail');

中间件

要注册 IsEmailVerified 中间件,请在 app/Http/Kernel.php 文件中的 $routeMiddleware 数组中添加以下内容

protected $routeMiddleware = [
    // …
    'isEmailVerified' => \Lunaweb\EmailVerification\Middleware\IsEmailVerified::class,

在您的路由上应用中间件

Route::group(['middleware' => ['web', 'auth', 'isEmailVerified']], function () {
    …

事件

此包发出 2 个事件

  • Lunaweb\EmailVerification\Events\EmailVerificationSent
  • Lunaweb\EmailVerification\Events\UserVerified

重新发送验证邮件

使用 isEmailVerified 中间件,以下表单将显示给用户。它允许用户更正其电子邮件地址并重新发送验证邮件。

Screenshot

您可以使用 showResendVerificationEmailForm 路由手动将用户指向此表单(默认:register/verify/resend)。

以编程方式重新发送验证邮件

$this->app->make('Lunaweb\EmailVerification\EmailVerification')->sendVerifyLink($user);

自定义验证邮件

因此,覆盖您用户模型的 sendEmailVerificationNotification() 方法。示例

class User implements CanVerifyEmailContract
{

    use CanVerifyEmail;

    /**
     * Send the email verification notification.
     *
     * @param  string  $token   The verification mail reset token.
     * @param  int  $expiration The verification mail expiration date.
     * @return void
     */
    public function sendEmailVerificationNotification($token, $expiration)
    {
        $this->notify(new MyEmailVerificationNotification($token, $expiration));
    }
}

自定义重新发送表单

php artisan vendor:publish --provider="Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider" --tag="views"

模板位于 resources/views/vendor/emailverification/resend.blade.php

自定义消息/本地化

php artisan vendor:publish --provider="Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider" --tag="translations"

本地化文件位于 resources/lang/vendor/emailverification