Abdelhamid
My Latest articles

Implementing Google reCAPTCHA in Laravel using Livewire

Secure your Laravel application with Google reCAPTCHA using this guide. you will walk through adding reCAPTCHA to forms, step-by-step, using livewire.

Implementing Google reCAPTCHA in Laravel using Livewire's cover image

Introduction

This article is an updated version, of the previous one here

Google reCAPTCHA is a captcha like system. 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.

In this article, we will discuss how to implement Google reCAPTCHA in Laravel livewire projects. Just follow the below given easy steps to add Google reCAPTCHA into your livewire form, and you are ready to go.

Note If you are already an expert, and know how to deal with Laravel, and livewire. Just jump into the 9th step

I will guide you through these whole steps, all what I need from you to just bear with me. :)

What is Laravel Livewire?

Livewire is a full-stack framework for Laravel that makes building dynamic interfaces simple, without leaving the comfort of Laravel.

Step 1: Download Laravel app

the first step you're gonna to need is the installation of a fresh Laravel app (if you don't have an existing project) and I will be using Laravel ^10 and Livewire ^2

composer create-project --prefer-dist laravel/laravel recaptcha-app

Or

laravel 

after the installation goes well, you need to run NPM installation to scaffold the front-end

npm install && npm run dev

Step 2: Add Database Credentials

You need to add the database credentials to store the messages after the user submitting the form.

In this case, I'll be using MySQl as a database driver.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=recaptcha_app
DB_USERNAME=database_username
DB_PASSWORD=database_password

Step 3: Create Contact Model

After setting up the database, we need to create a new model called Contact by running:

php

The -m Flag is for creating a migration file.

For the sack of this example, I'll keep it simple, I just added an email field and a body

public

After that, we add the fillable fields in the Contact model.

class

Step 5: Run Migration Command

After creating the project, adding the database credentials, and adding the model, we must run the migration command by:

php artisan migrate

and after the migration, you will see in the contacts' table something like this:

Step 6: Install livewire to your existing project

We arrived at the exciting part 😊 which is installing Laravel livewire.

composer 

After the installation process completed successfully, We will add the following blade directives in the head tag, and before the end body tag in your template.

<

You can alternatively use the tag syntax.

<

After that, we need to publish the config file and the Frontend Assets

php

Step 7: Generate livewire Component

After we're successfully installing Livewire into our project, We need to generate a new component:

php

This command will generate 2 files,

CLASS

Step 8: Add Routes

Now we need to add some routes into our application to show the contact form.

In your routes/web.php add the following:

Route::get(

Step 9: add Google reCAPTCHA

In this part we will be using reCAPTCHA V2 and all what you need to do is the following:

First add the captcha key and secret into the .env file

CAPTCHA_SITE_KEY=xxxx
CAPTCHA_SECRET_KEY=xxxx

and in contact.blade.php

<

after we place the form, we must add google captcha scripts

 

and in your Contact Component do the following

 

Once you verify that you are not a bot, the script above will check, and send an encrypted token to your backend to verify, and return if there are any issues as a session error on the captcha attribute.

Step 10: Run Development Server

After setting up all the dependencies, run your development server by:

php artisan serve

Or any kind of methods you like, such as valet or homestead … Etc

Conclusion

In this article we take a look at Google reCAPTCHA and how to implement it using Laravel and Laravel livewire, for more information about google reCAPTCHA, you can take a look at the official documentation.

Thank you for stopping by, and I hope you find something useful.

Let's make something creative together!

Book a meet

I'm a software developer, I love building web applications and solve problems. I'm always looking for new challenges and opportunities to learn and grow.

©2024 Abdelhamid Errahmouni, All rights reserved.