首页 > 解决方案 > 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));
    }

标签: phplaravellaravel-5.8

解决方案


我建议在您的 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]
    );
}

推荐阅读