Categories
Uncategorized

Adding Recaptcha to Laravel Project

Using this package here: https://github.com/anhskohbo/no-captcha

composer require anhskohbo/no-captcha
This gave us the error:

Could not fetch https://api.github.com/repos/symfony/polyfill-mbstring/zipball/2 ec8b39c38cb16674bbf3fea2b6ce5bf117e1296, please create a GitHub OAuth token to g o over the API rate limit
Head to https://github.com/settings/tokens/new?scopes=repo&description=Composer+ on+laptop_01-PC+2018-02-20+2229

To fix that we simply retrieved an API token from the URL referenced in the message, pasted it into the command line and hit enter.

In app/config/app.php add the following :

1- The ServiceProvider to the providers array :

Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class,

2- The class alias to the aliases array :

'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,

3- Publish the config file

php artisan vendor:publish --provider="Anhskohbo\NoCaptcha\NoCaptchaServiceProvider"

Add NOCAPTCHA_SECRET and NOCAPTCHA_SITEKEY in .env file :

NOCAPTCHA_SECRET=secret-key
NOCAPTCHA_SITEKEY=site-key

4- In templates frontend.blade add this snippet before /head:
{!! NoCaptcha::renderJs() !!}

5- In contact_us.blade add:

{!! NoCaptcha::display() !!}

in the actual form section:

@if ($errors->has('g-recaptcha-response'))

{{ $errors->first('g-recaptcha-response') }}

@endif

6- In The contact us controller, add the validation requirement to the array of requirements:
'g-recaptcha-response' => 'required|captcha',

7- Now we have everything installed correctly on the form, it should prevent a submission unless the recaptcha is filled out. Once submitted though the form will now error out with the error below:
cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

Lets fix this error:
Go to http://curl.haxx.se/ca/cacert.pem and download the pem file and save in your php installation directory (C:\wamp64\bin\php) make sure while saving it retains the extension and not saved as a text file.

Now, open your php.ini file, scroll to the bottom and add the following line:

[cURL]
curl.cainfo="C:\wamp64\bin\php\cacert.pem"

Replace the file path above with the path to the certificate on your machine
*once you save the php.ini file restart all services on wamp*