Add recaptcha to default Laravel Password Reset

2019-06-14 14:45发布

问题:

I want to require the users of my Laravel 5.1 application to have finished a Google Recaptcha process, but I can't figure out how to safely modify the code that sends the reset password link.

The code that does this for me is the "postEmail()" function in the inherited trait "ResetsPassword". This is my entire 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');
}

}

As you can see, all the real methods are in the "ResetsPasswords" trait which is in a vendor file so I don't want to modify it directly. How do I modify the "postEmail()" function in the inherited trait safely in my PasswordsController?

回答1:

In your ForgotPasswordController add this method:

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

And follow my reCAPTCHA implementation guide here: Laravel reCaptcha integration