【问题标题】:Laravel Custom PasswordBroker not sending password reset link emailLaravel Custom PasswordBroker 不发送密码重置链接电子邮件
【发布时间】:2020-05-19 01:09:18
【问题描述】:

我在 Laravel 中使用自定义密码重置功能。 Laravel version 5.8

我关注了这份文件https://laravel.com/docs/5.8/passwords#password-customization 并且还添加了我在这里检查过的自定义保护https://laravel.com/docs/5.8/authentication#adding-custom-guards

我不知道我在下面的这些设置中做错了什么。

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'customers' => [
            'driver' => 'session',
            'provider' => 'customers',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'customers' => [
            'driver' => 'eloquent',
            'model' => App\Models\CustomerLoginInfo::class,
        ],

        'users' => [
            'driver' => 'database',
            'table' => 'users',
        ],
    ],

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

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

        'customers' => [
            'provider' => 'customers',
            'table' => 'customer_password_resets',
            'expire' => 60,
        ],
    ],

];

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;

class ForgotPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset emails and
    | includes a trait which assists in sending these notifications from
    | your application to your users. Feel free to explore this trait.
    |
    */

    use SendsPasswordResetEmails;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    protected function broker()
    {
        return Password::broker('customers');
    }

    /**
     * Validate the email for the given request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    protected function validateEmail(Request $request)
    {
        $request->validate(['login_email' => 'required|email']);
    }

    /**
     * Get the needed authentication credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        return $request->only('login_email');
    }
}

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Password;

class ResetPasswordController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */

    use ResetsPasswords;

    /**
     * Where to redirect users after resetting their password.
     *
     * @var string
     */
    protected $redirectTo = '/form/03';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest:customers');
    }

    protected function guard()
    {
        return Auth::guard('customers');
    }

    protected function broker()
    {
        return Password::broker('customers');
    }
}

我的模型扩展了可验证性

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class CustomerLoginInfo extends Authenticatable
{
    use Notifiable;
    use SoftDeletes;

    protected $table = 'customer_login_info';

    protected $fillable = ['customer_id', 'login_email', 'password'];

    public function customer()
    {
        return $this->belongsTo('App\Models\Customer');
    }

    public function getEmailForPasswordReset()
    {
        return $this->login_email;
    }
}

即使我收到成功消息,我也没有收到密码重置链接电子邮件。

我正在使用 docker mailhog,接收其他类型的邮件没有任何问题。

如果您发现此代码中有任何奇怪之处,请告诉我。

【问题讨论】:

  • 请求后是否有任何记录添加到您的customer_password_resets 表中?
  • @HafezDivandari 是的,电子邮件地址、令牌和时间戳。
  • 您是否使用队列发送邮件?如果是,是否有任何记录添加到您的 jobsfailed_jobs 表中?关于storage/logs的任何日志报告?
  • @HafezDivandari 不,我没有使用队列,所以我在 laravel 日志中看不到任何记录。
  • 尝试更改您的邮件驱动程序以记录并检查是否有任何电子邮件发送到日志。在您的 .env 文件上更改此 MAIL_DRIVER=log

标签: php laravel docker email


【解决方案1】:

回复自己,我找到了解决方案,我研究了这个问题几个小时,在 Laravel 5.8 中你需要

public function routeNotificationForMail($notification)
{
        return $this->login_email;
}

在您的可验证中,如果您使用的电子邮件列的名称不是电子邮件。

https://laravel.com/docs/5.8/notifications#customizing-the-recipient

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-28
    • 2016-04-04
    • 1970-01-01
    • 2016-03-01
    • 2013-12-18
    • 2015-08-13
    • 2014-02-08
    • 2015-11-26
    相关资源
    最近更新 更多