试图重置流明中的密码

时间:2016-05-20 04:35:32

标签: laravel lumen

我正在尝试在Lumen中实现密码重置功能,但无法成功。

Lumen可以访问Laravel的 PasswordBroker PasswordManager 等内容,但我无法使用此功能并取得成功。有没有解决方案。

2 个答案:

答案 0 :(得分:2)

我昨晚真的想出了这个,写了一篇关于它的博客:

http://www.imjohnbon.com/password-resets-in-lumen/

希望它至少能让你走上正轨。

答案 1 :(得分:2)

好的,所以我自己使用Lumen 5.6进行了这项工作。这些是我必须对代码进行的更改。我必须调整一些东西以适合我的系统,但这可能会对其他人有所帮助。

路线

我想要两条路线。

  1. 发送重置密码电子邮件

Class

  1. 重置密码

GET auth/resetpassword?email=fred@example.com

作曲家

要向用户发送电子邮件,需要以下Laravel软件包:

PUT auth/resetpassword?token=281...b&username=fred&password=banana&password_confirmation=banana

配置文件

需要一些额外的配置文件:

  • config / auth.php
  • config / mail.php

我只是从Laravel 5.6复制了这些内容,没有进行任何更改。我还将邮件变量添加到了 .env 文件中。

bootstrap / app.php

这些新文件需要在应用程序中注册:

composer require illuminate/mail "^5.6"
composer require illuminate/notifications "^5.6"

数据库

我使用了默认的Laravel password_resets表:

$app->configure('auth');
$app->configure('mail');

$app->register(Illuminate\Notifications\NotificationServiceProvider::class);
$app->register(\Illuminate\Mail\MailServiceProvider::class);
$app->alias('mailer', \Illuminate\Contracts\Mail\Mailer::class);
$app->withFacades();

现在对代码进行真正的更改。

基于 Illuminate \ Foundation \ Auth \ SendsPasswordResetEmails Illuminate \ Foundation \ Auth \ ResetsPasswords 中的代码。

AuthController

Schema::create('password_resets', function (Blueprint $table) {
    $table->string('email')->index();
    $table->string('token');
    $table->timestamp('created_at')->nullable();
});

用户模型

use Illuminate\Auth\Passwords\PasswordBrokerManager;
use Illuminate\Support\Facades\Password;
use Laravel\Lumen\Routing\Controller;
use Illuminate\Http\Request;

class AuthController extends Controller
{
    // 1. Send reset password email
    public function generateResetToken(Request $request)
    {
        // Check email address is valid
        $this->validate($request, ['email' => 'required|email']);

        // Send password reset to the user with this email address
        $response = $this->broker()->sendResetLink(
            $request->only('email')
        );

        return $response == Password::RESET_LINK_SENT
            ? response()->json(true)
            : response()->json(false);
    }

    // 2. Reset Password
    public function resetPassword(Request $request)
    {
        // Check input is valid
        $rules = [
            'token'    => 'required',
            'username' => 'required|string',
            'password' => 'required|confirmed|min:6',
        ];
        $this->validate($request, $rules);

        // Reset the password
        $response = $this->broker()->reset(
        $this->credentials($request),
            function ($user, $password) {
                $user->password = app('hash')->make($password);
                $user->save();
            }
        );

        return $response == Password::PASSWORD_RESET
            ? response()->json(true)
            : response()->json(false);
    }

    /**
     * Get the password reset credentials from the request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return array
     */
    protected function credentials(Request $request)
    {
        return $request->only('username', 'password', 'password_confirmation', 'token');
    }

    /**
     * Get the broker to be used during password reset.
     *
     * @return \Illuminate\Contracts\Auth\PasswordBroker
     */
    public function broker()
    {
        $passwordBrokerManager = new PasswordBrokerManager(app());
        return $passwordBrokerManager->broker();
    }
}