将recaptcha添加到默认的Laravel密码重置

时间:2017-10-02 13:25:50

标签: laravel laravel-5 laravel-5.1 recaptcha change-password

我想要求Laravel 5.1应用程序的用户完成Google Recaptcha流程,但我无法弄清楚如何安全地修改发送重置密码链接的代码。

为我这样做的代码是" postEmail()"功能在继承的特征" ResetsPassword"。这是我的整个PasswordController:

use App\Http\Controllers\Controller;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Contracts\Auth\PasswordBroker;
use Illuminate\Foundation\Auth\ResetsPasswords;

class PasswordController extends Controller {

use ResetsPasswords;

/**
 * Create a new password controller instance.
 *
 * @param  \Illuminate\Contracts\Auth\Guard  $auth
 * @param  \Illuminate\Contracts\Auth\PasswordBroker  $passwords
 * @return void
 */
public function __construct(Guard $auth, PasswordBroker $passwords)
{
    $this->auth = $auth;
    $this->passwords = $passwords;

    $this->middleware('guest');
}

}

正如您所看到的,所有真正的方法都在" ResetsPasswords"特权在供应商文件中,所以我不想直接修改它。如何修改" postEmail()"在我的PasswordsController中安全地在继承的特征中运行?

2 个答案:

答案 0 :(得分:2)

ForgotPasswordController添加此方法:

protected function validateEmail(Request $request)
{
    $this->validate($request, [
        'email' => 'required|email',
        'g-recaptcha-response' => 'recaptcha',
    ]);
}

请按照我的reCAPTCHA实施指南:Laravel reCaptcha integration

答案 1 :(得分:0)

将代码添加到 Auth / ForgotPasswordController.ph p

<?php

namespace App\Http\Controllers\Auth;

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

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 validateEmail(Request $request)
    {
        $this->validate($request, [
            'email' => ['required', 'string', 'email', 'max:255'],
            'g-recaptcha-response' => 'required|recaptcha',
        ]);
    }
}