php - laravel 中的密码重置链接有效期不会过期
问题描述
我创建了 ResetPassword.php 的本地化副本,重新定义sendPasswordResetNotification ()
了app\user
. 总的来说,我按照规则做所有事情(我认为是这样)))。所有的规则,发送一封信到邮件,但只有一个问题。此链接不会过期。默认情况下有 60 分钟,之后链接应该被销毁。但这不会发生。在我的代码下方将不胜感激。
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class mytelresetpassword extends Notification
{
use Queueable;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($token)
{
//
$this->token = $token;
}
/**
* The callback that should be used to build the mail message.
*
* @var \Closure|null
*/
public static $toMailCallback;
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
if (static::$toMailCallback) {
return call_user_func(static::$toMailCallback, $notifiable, $this->token);
}
$time = ['count' => config('auth.passwords.'.config('auth.defaults.passwords').'.expire')];
$time = $time['count'];
return (new MailMessage)
->subject('Şifrənin Dəyişdirilməsi')
->line('Hesabınızdan şifrə yeniləmə tələbi aldığımız üçün bu mail sizə göndərilib.')
->action('Şifrəni Yenilə', url(config('app.url').route('password.reset', ['token' => $this->token, 'email' => $notifiable->getEmailForPasswordReset()], false)))
->line('Bu link '.$time.' dəqiqə müddətində keçərlidir, bu müddət bitdikdən sonra deaktiv olunacaq!')
->line('Şifrə dəyişməyi tələb etməmişsinizsə, "Şifrəni Yenilə" düyməsini klikləməyə ehtiyac yoxdur.');
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
//
];
}
/**
* Set a callback that should be used when building the notification mail message.
*
* @param \Closure $callback
* @return void
*/
public static function toMailUsing($callback)
{
static::$toMailCallback = $callback;
}
}
在app\user
:
public function sendPasswordResetNotification($token)
{
$this->notify(new mytelresetpassword($token));
}
解决方案
我建议在您的 ResetPasswordController.php 中添加几行:
public function showResetForm(Request $request, $token = null)
{
$email = $request->email;
if (!$token)
return redirect()->route('login');
if (!$email)
return redirect()->route('login');
$reset = PasswordReset::where('email', $request->input('email'))->first();
if (!$reset)
return redirect()->route('login');
$expiry = Carbon::now()->subMinutes( 60 );
if ($reset->created_at <= $expiry) {
return redirect()->route('login');
}
return view('auth.passwords.reset')->with(
['token' => $token, 'email' => $request->email]
);
}
推荐阅读
- javascript - 从后端发送 pdf 的问题
- react-native - 我可以在单反应原生项目中同时使用 redux 和 Flux 吗?
- python - AWS Lambda中的Python:多处理,一个完成时终止其他进程
- awk - pattern1 和第一个 pattern2 之间的双引号匹配并替换文件中的换行符(输出整个文件)
- python - __repr__() 和仅调用类实例之间的区别
- python - Snakemake:横向 DAG 深度优先?
- nginx - nginx 服务器块配置后出现文件未找到错误
- rust - Rust:从模块函数返回结果:类型参数的数量错误:预期 2,找到 0
- angular - 无法通过 webUSB 打开 USB 设备
- node.js - 在 node.js 中运行黄瓜多个标签