通过 url [Laravel Passport] 中的电子邮件发送令牌以重置密码

Send token to reset password via email in url [Laravel Passport]

我有一种方法可以在用户丢失密码时发送电子邮件,电子邮件已发送,并且我在 Angular 视图中为自定义 url 配置了通知。但我的问题是关于如何在 url 中发送令牌,例如 http://localhost:4200/password?token={TOKEN} 或其他类似方式。到目前为止我有这个:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

class MailResetPasswordToken extends Notification
{
    use Queueable;
    public $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        return (new MailMessage)
        ->subject(Lang::get('Change password'))
          ->line('You are receiving this email because we received a password reset request for your account.')
         ->action('Reset Password', url('http://localhost:4200',[$this->token], false))
         ->line('If you did not request a password reset, no further action is required.');           
         ->action('Reset Password', url(config('app.url').route('password.reset', [$this->token, $notifiable->email], false)))
   
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

您可以使用以下代码在 url 中发送令牌。

$url = url('/password?token=' . $this->token);

最后,您将 $url 传递给 ->action()

示例:

<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Lang;

class MailResetPasswordToken extends Notification
{
    use Queueable;
    public $token;

    public function __construct($token)
    {
        $this->token = $token;
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        $url = url('/password?token=' . $this->token);
        return (new MailMessage)
          ->subject(Lang::get('Change password'))
          ->line('You are receiving this email because we received a password reset request for your account.')
          ->action('Reset Password', $url)
          ->line('If you did not request a password reset, no further action is required.');
   
    }

    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}