Laravel 5.7电子邮件验证到期时间

时间:2018-12-05 15:48:56

标签: laravel laravel-5.7

我想自定义用户通过内置Auth验证其电子邮件地址的时间(从5.7开始)。

config/auth中有:

'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],

但是我没有发现类似的电子邮件验证内容。 the official documentation也没有提及。

3 个答案:

答案 0 :(得分:4)

如果您打开Illuminate\Auth\Notifications\VerifyEmail::class;

生成URL的方法已经使用了默认为1小时的到期时间。不幸的是,无法修改该值。

/**
 * Get the verification URL for the given notifiable.
 *
 * @param  mixed  $notifiable
 * @return string
 */
protected function verificationUrl($notifiable)
{
    return URL::temporarySignedRoute(
        'verification.verify', Carbon::now()->addMinutes(60), ['id' => $notifiable->getKey()]
    );
}

答案 1 :(得分:3)

实际上,Laravel中没有选项,但是由于laravel使用以下内容:

  • 特征MustVerifyEmail(在Illuminate\Foundation\Auth\User类中,由主要User模型扩展)

  • 事件和通知

MustVerifyEmail特性中,有一种称为sendEmailVerificationNotification的方法。在此使用@nakov's answer引用的Notification VerifyEmail类及其功能verificationUrl

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    $this->notify(new Notifications\VerifyEmail);
}

由于我们知道这一点,因此我们可以执行以下操作:

  • Notifications\VerifyEmail扩展到我们的自定义VerifyEmail
  • 覆盖verificationUrl的实现
  • 重写sendEmailVerificationNotification模型中User方法的实现,以使用我们新的VerifyEmail类。

完成上述操作后,我们的User模型将具有以下方法:

/**
 * Send the email verification notification.
 *
 * @return void
 */
public function sendEmailVerificationNotification()
{
    $this->notify(new \App\Services\Verification\VerifyEmail);
}

现在,我们使用自定义的VerifyEmail类。然后我们新的VerifyEmail类将如下所示:

namespace App\Services\Verification;

use Illuminate\Support\Carbon;
use \Illuminate\Support\Facades\URL;

class VerifyEmail extends \Illuminate\Auth\Notifications\VerifyEmail
{
    protected function verificationUrl($notifiable)
    {
        return URL::temporarySignedRoute(
            'verification.verify', Carbon::now()->addMinute(3), ['id' => $notifiable->getKey()]
        );  //we use 3 minutes expiry
    }
}

除了解释之外,该过程非常简单。我希望很容易掌握。干杯!

答案 2 :(得分:1)

尽管问题专门针对Laravel 5.7,但我觉得值得一提的是,从Laravel 5.8开始,可以通过config变量实现这一点。我自定义验证到期时间的搜索将这个问题作为最高结果返回,因此添加了我。

如果我们检出Illuminate\Auth\Notifications\VerifyEmail,则verificationUrl方法现在看起来像这样:

protected function verificationUrl($notifiable)
{
    return URL::temporarySignedRoute(
        'verification.verify',
        Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
        ['id' => $notifiable->getKey()]
    );
}

这样,我们可以将此块添加到config/auth.php来自定义时间,而无需扩展类或其他任何东西:

'verification' => [
    'expire' => 525600, // One year - enter as many mintues as you would like here
],
相关问题