Blog Detail

04

Aug
A New Laravel Package for Google reCAPTCHA V3 cover image

arrow_back A New Laravel Package for Google reCAPTCHA V3

Google reCAPTCHA v3 is a new mechanism to verify whether the user is a bot or not. It has been designed for potential users, site owners that want more data about their traffic. Google reCAPTCHA v3 returns a score for each request without user friction. The score is based on interactions with your site and allows you to take appropriate action for your site. Register reCAPTCHA v3 keys here.

You can also visit this:
https://developers.google.com/recaptcha/docs/faq

Introduction

RyanDaDeng introduced a Laravel Package for Google reCAPTCHA V3 that has the following features.

  • High Test coverage, safe and easy to use
  • Score Comparision
  • Support invisible, corner, and inline badge style
  • Support reCAPTCHA to run on every page
  • Support multiple actions to be placed on the same page
  • Support custom implementation on config interface
  • Support custom implementation on request method interface
  • Fully supported Vue component
  • IP skip list supported

Requirement

This package requires the following dependencies:

  • Laravel framework should be greater or equals to Laravel 5.
  • If you want to use Validation Class then your Laravel version needs to be >= 5.5
  • Php should be greater than Php 5
  • You must have read basic information from Google reCAPTCHA v3.

Installation

You can install it via composer:

$ composer require timehunter/laravel-google-recaptcha-v3 "~2.4.3" -vvv

If your Laravel framework version is less than or equal to Laravel 5.4, then please register the service provider under your config file: /config/app.php, otherwise please skip it.

'providers'=[
    ....,
TimeHunter\LaravelGoogleReCaptchaV3\Providers\GoogleReCaptchaV3ServiceProvider::class
]

And also

'aliases'=[
     ....,
     'GoogleReCaptchaV3'=> TimeHunter\LaravelGoogleReCaptchaV3\Facades\GoogleReCaptchaV3::class
 ]

If your Laravel framework version is greater than or equals to laravel 5.5, just run the following command to publish config.

$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV3\Providers\GoogleReCaptchaV3ServiceProvider" --tag=googlerecaptchav3.config

For vue component you’ve to run this command:

$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV3\Providers\GoogleReCaptchaV3ServiceProvider" --tag=googlerecaptchav3.vuejs

After installation, you should see a googlerecaptchav3.php in your app/config folder, and a vue component under js/components/googlerecaptchav3 folder.

For multi lang you’ve to run this command:

$ php artisan vendor:publish --provider="TimeHunter\LaravelGoogleReCaptchaV3\Providers\GoogleReCaptchaV3ServiceProvider" --tag=googlerecaptchav3.lang

A lang folder will be created under /resources/lang/vendor/GoogleReCaptchaV3/

Configurations

Setting up your Google reCAPTCHA details in the config file

You have to register all details in the config for host_name, site_key, secret_key, and site_verify_url.
Register credentials in .env:

RECAPTCHA_V3_SECRET_KEY=
RECAPTCHA_V3_SITE_KEY=

Specify your Score threshold and action in ‘setting’, e.g.

      'setting' =  [
          [
                'action' => 'contact_us', // Google reCAPTCHA required paramater
                'threshold' => 0.2, // score threshold
                'score_comparision' => false // if this is true, the system will do score comparsion against your threshold for the action
            ],
            [
                'action' => 'signup',
                'threshold' => 0.2,
                'score_comparision' => true
            ],
        ]

Note: if you want to enable Score Comparision, you also need to enable is_score_enabled to be true.

        ...
        'is_score_enabled' = true
        ...

For Blade & Vue Usage you can view its detailed documentation on Github.

Published at : 04-08-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project