yaquawa/laravel-email-reset

Laravel 重置邮箱包。

v1.0.8 2020-03-26 12:15 UTC

This package is auto-updated.

Last update: 2024-08-29 04:26:00 UTC


README

通过发送验证链接到新邮箱来重置用户邮箱的 Laravel 包。

此包通过以下方式重置用户邮箱:

  1. 向用户的新邮箱发送验证链接。
  2. 如果用户点击新邮箱中的验证链接,该包将验证新邮箱并将旧邮箱设置为新的。

如何安装

要开始使用,请按照以下步骤操作。

安装

composer require yaquawa/laravel-email-reset

配置

将以下代码添加到您的 <config/auth.php> 文件中。

'defaults' => [
    'guard'       => 'web',
    'passwords'   => 'users',
    'email-reset' => 'default' // Add this line
],

// Add this entire block
'email-reset' => [
    'default' => [
        'table'  => 'email_resets',
        'expire' => 60,
        'callback' => 'App\Http\Controllers\Auth\ResetEmailController@reset',
        // 'ignore-migrations' => true,
        // 'route' => 'email/reset/{token}',
    ]
]

迁移

php artisan migrate

如果您想使用自己的迁移,在配置文件中将 ignore-migrations 设置为 true(参见迁移文件 此处)。

发布资源

以下命令将包的控制器和翻译文件发布到您的应用程序目录中。

php artisan vendor:publish --tag=laravel-email-reset

使用 CanResetEmail 特性

在您的 app/Models/User.php 文件中,使用 CanResetEmail 特性。此特性为用户模型添加了 resetEmail 方法。

namespace App\Models;

use Yaquawa\Laravel\EmailReset\CanResetEmail;

class User extends Authenticatable
{
    use CanResetEmail;
}

用法

发送验证邮件

// By calling the `resetEmail` method of `User` instance,
// an verification email will be sent to the user's new email address.
// If the user clicked the verification link, the new email address will be set.

// * The route of the verification link can be set at `route` in the config file.
 
$user->resetEmail('new_email@example.com');

如果您想更改邮件内容,您可以在 AppServiceProvider.php 中进行如下操作。

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Notifications\Messages\MailMessage;
use Yaquawa\Laravel\EmailReset\Notifications\EmailResetNotification;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        EmailResetNotification::toMailUsing(function ($user, $token, $resetLink) {
            return (new MailMessage)
                ->line('You are receiving this email because we received a email reset request for your account.')
                ->action('Reset Email', $resetLink)
                ->line('If you did not request a email reset, no further action is required.');
        });
    }
}

用户点击验证链接后,默认情况下,用户将被重定向到应用程序的根 URL。您可以通过覆盖已发布的控制器 ResetEmailController 的方法来更改此行为。

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Yaquawa\Laravel\EmailReset\ResetEmail;

class ResetEmailController extends Controller
{
    use ResetEmail;

    /**
     * This method will be called if the token is invalid.
     * Should return a response that representing the token is invalid.
     *
     * @param $status
     *
     * @return mixed
     */
    protected function sendResetFailedResponse(string $status)
    {
        return redirect($this->redirectPathForFailure())->withErrors(['laravel-email-reset' => trans($status)]);
    }

    /**
     * This method will be called if the token is valid.
     * New email will be set for the user.
     * Should return a response that representing the email reset has succeeded.
     *
     * @param $status
     *
     * @return mixed
     */
    protected function sendResetResponse(string $status)
    {
        return redirect($this->redirectPathForSuccess())->with('laravel-email-reset', trans($status));
    }

    /**
     * The redirect path for failure.
     *
     * @return string
     */
    protected function redirectPathForFailure(): string
    {
        return '/';
    }

    /**
     * The redirect path for success.
     *
     * @return string
     */
    protected function redirectPathForSuccess(): string
    {
        return '/';
    }
    
}

检索“新邮箱”

新邮箱将在用户点击验证链接之前不会保存。在用户点击验证链接之前,您可以通过以下方式获取新邮箱:

$user->new_email; // retrieve the `new_email` from database