Laravel 9 Google ReCAPTCHA Tutorial Example

Hello Artisan,

In this google recaptcha validation in laravel 9 tutorial, I will show you step by step how we can implement Laravel 9 form validation before submitting with google recaptcha v2. I will use anhskohbo/no-captcha captcha package.

Sometimes we need to protect our form from boot traffic form submission. For that reason, we need form validation before submitting. So we will see step by step guide on laravel 9 google recaptcha.

Google ReCaptcha is a captcha-like system that provides security against hackers and sticks or curls requests. It assures that a computer user is a human. It is the best and most used captcha system available where users are only required to click on a checkbox and in some cases select some similar images related to the conman question.

We use google recaptcha to increase our website security. We can check humans or robots using Google Recaptcha. So let's do google Recaptcha code with validation in laravel 9.

In this example, we will create a simple registration form and will try to create a google captcha code. before using the google captcha code we will install anhskohbo/no-captcha the composer package for google captcha in our application. You have to just follow a few step and you will get google re-captcha code in your laravel 9 application.

 

Step 1 : Download Laravel 9

first of all, we need to get a fresh Laravel 9 version application using below command, So open your terminal OR command prompt and run below command:

composer create-project --prefer-dist laravel/laravel blog

 

Step 2: Install anhskohbo/no-captcha Package

In this step, we need to install anhskohbo/no-captcha via the Composer package manager, so one your terminal and fire bellow command

composer require anhskohbo/no-captcha

 

After successfully installing the package, we require to add aliases and service providers.

Config/app.php

 [
         Anhskohbo\NoCaptcha\NoCaptchaServiceProvider::class
     ],

     'aliases' => [
         'NoCaptcha' => Anhskohbo\NoCaptcha\Facades\NoCaptcha::class,
     ]
  ]

 

Step 3: Update Google API Key

In this step, we need to set the google site key and secret key. If you don't have a site key and secret key then you can create from here. First click on this link: Recaptcha Admin

After click, you can see the bellow view and you need to register your site link this way:

laravel-9-recaptcha-code-with-validation

 

Now open the .env file and add these two variable

.env

NOCAPTCHA_SITEKEY=[site-key]
NOCAPTCHA_SECRET=[secret-key]

 

Step 4: Add Route

We will use the Laravel default auth mechanism to complete our project. So we have no need to create a new controller. So go to your RegisterController.php. In your register controller, your default validator function will look like this. 

 app/Http/Controllers/Auth/RegisterController.php

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed'
        ]);
    }

 

Now you have to just add one line of code. see the below code

 protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|string|max:255',
            'slug' => 'required',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|min:6|dumbpwd|confirmed',
            'g-recaptcha-response' => 'required|captcha',
        ]);
    }

 

Look we just added 'g-recaptcha-response' => 'required|captcha' line to get Capctcha code

 

Step 5: Create View File

This is the last step. So go to your register.blade.php and add the following code to your registration form.

 resources/views/auth/register.blade.php

 

Read also: Captcha Code and Validation Example using Botdetect Package in Laravel

 

Finally, our google Recaptcha form validation in laravel 9 is over. Hope it can help you.

 

#laravel #laravel-9x