Laravel add reCAPTCHA
I assume you had already set up Laravel prior to reading this. If you have not please read my how to set up Laravel blog post to set up Laravel in Ubuntu 16.04
Create a scaffold of the authentication system using the command below so that we can add reCAPTCHA to the registration, login and change password page. Once you run the commands, you will see login and register on the top right corner and it is fully functional.
sudo php artisan make:auth sudo php artisan migrate
Add validation rule
You will need to run the artisan to create a validation rule class for your reCAPTCHA. Once you run the command below, you will notice that in your Laravel app/Rules/ folder will contain a Recaptcha.php file. Edit the file so that it looks like my example.
sudo php artisan make:rule Recaptcha
Edit the composer.json file and add the line “guzzlehttp/guzzle”: “6.*”, within “require-dev”. After that update composer by calling the line below.
sudo composer update
Obtain reCAPTCHA keys
Go to Google reCAPTCHA and request for a set of keys with your domain for your website use.
Edit environment variables
Edit the .env file in Laravel and add two variables into the file. These two variables are the values for the keys you obtained from Google reCAPTCHA.
Add client site integration code
Go to resources/views/auth/ and edit the file register.blade.php. Add the codes given to you when you request for Google reCAPTCHA keys. As for the site key, I had it replaced with the environment variables we added earlier.
<div class="g-recaptcha" data-sitekey="6LeSB2YUAAAAAJnevyfLs4hlqnsfFgaGwH7Xtl8M"></div>
Implement validation rules
To use the rule implemented earlier, you will need to edit the file RegistersUsers.php located at “vendor/laravel/framework/src/Illuminate/Foundation/Auth”. Add “use App\Rules\Recaptcha;” at the top to include the rule created earlier. In the register function, you will need to get the IP of the incoming request and submit it to reCAPTCHA for validation.