Laravel6 - How to implement invisible reCAPTCHA using albertcht/invisible-recaptcha
What is google invisible reCAPTCHA
Google reCAPTCHA is a google's product is can help us provide more security against spam and other types of automated abuse. The invisible reCAPTCHA badge does not require the user to click on a checkbox, instead, it is invoked directly when the user clicks on an existing button on your site or can be invoked via a JavaScript API call. The integration requires a JavaScript callback when reCAPTCHA verification is complete. By default, only the most suspicious traffic will be prompted to solve a captcha. To alter this behavior edit your site security preference under advanced settings.
If you never integrate google invisible reCAPTCHA in your laravel application before don't worry here we will share with you how to implement google invisible reCAPTCHA in your laravel application step by step.
Step - 1 Install package
First, we need to install albertcht/invisible-recaptcha
laravel package in your laravel application using the following command in your project's root directory.
composer require albertcht/invisible-recaptcha
Step - 2 Setup
After installing the package, open your Laravel config file located at config/app.php
and add the following lines.
In the $providers
array add the following service provider for this package.
AlbertCht\InvisibleReCaptcha\InvisibleReCaptchaServiceProvider::class,
Step - 3 Configuration
Now, we need to create one site in google recaptcha console and get our required keys. please click on this link and do it first https://www.google.com/recaptcha/admin/create. please also see the following screenshot it can help you more.
Make sure you have checked to belove invisilbe reCAPTCHA badge
option during the create a new one site. after submitting this form then you can redirect one another page. their you can get your siteKey
and secretKey
.
Now, open your .env
file and set your both key into it. just copy the following code and use it.
#required
INVISIBLE_RECAPTCHA_SITEKEY={siteKey}
INVISIBLE_RECAPTCHA_SECRETKEY={secretKey}
#optional
INVISIBLE_RECAPTCHA_BADGEHIDE=false
INVISIBLE_RECAPTCHA_DATABADGE='bottomright'
INVISIBLE_RECAPTCHA_TIMEOUT=5
INVISIBLE_RECAPTCHA_DEBUG=false
Step - 4 How to use
Done all the above steps then into the last how to use it in laravel application. you can implement google invisible reCAPTCHA in any form of submission. which you want more secure and stop to spam data inserting.
In this example, I will implement google invisible reCAPTCHA in my registration form. you all know laravel provide readymade AUTH system.
just open your resources/views/auth/register.blade.php
file and simply put the following code snippet into your registration form.
{!! app('captcha')->render(); !!}
Now, we also make validation on google invisible reCAPTCHA when a user submits the registration form. just open your app\Http\Controllers\Auth\RegisterController.php
file and make the following changes into the validation section.
protected function validator(array $data)
{
return Validator::make($data, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'g-recaptcha-response' => 'required|captcha'
]);
}
Conclusion
As you can see, google invisible reCAPTCHA integration is very easy to use in laravel application help of albertcht/invisible-recaptcha
package. and provide protection against spam.
We hope these tutorials help everyone. if you have any issues or questions regarding google invisible reCAPTCHA integration so please comment below. Thanks..
Copyright 2024 ErrorGram