DEV Community

Tutsmake
Tutsmake

Posted on

Laravel 10 Google Recaptcha V3 Example

Now I can provide you with guidance on implementing Google reCAPTCHA v3 validation in Laravel 10.

Google reCAPTCHA v3 is a spam prevention service that allows you to protect your Laravel application against abusive activities, such as bots, without requiring users to solve CAPTCHA puzzles. Instead, it assigns a score to each request based on user behavior, and you can set a threshold to determine which requests are likely to be abusive.

Here's a step-by-step guide on how to implement Google reCAPTCHA v3 validation in Laravel:

Step 1: Set Up reCAPTCHA v3

Go to the Google reCAPTCHA website and sign in or create an account if you don't have one.

Register a new site. Choose the reCAPTCHA v3 option and provide the domain(s) where you'll be using reCAPTCHA. Google will provide you with a site key and a secret key.

Step 2: Install Laravel's Validation Package

If you haven't already installed Laravel's validation package, you can do so using Composer:

composer require laravel/fortify
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure reCAPTCHA in Laravel

In your Laravel project, open the .env file and add your reCAPTCHA site key and secret key:

RECAPTCHA_SITE_KEY=your_site_key
RECAPTCHA_SECRET_KEY=your_secret_key
Enter fullscreen mode Exit fullscreen mode

Step 4: Create a Blade View

Create a Blade view where you want to include the reCAPTCHA validation. For example, you can create a contact form:

<!-- resources/views/contact.blade.php -->
<!DOCTYPE html>
<html>
<head>
    <title>Contact Us</title>
    <script src="https://www.google.com/recaptcha/api.js" async defer></script>
</head>
<body>
    <h1>Contact Us</h1>
    <form action="{{ route('contact.submit') }}" method="POST">
        @csrf
        <label for="name">Name:</label>
        <input type="text" name="name" id="name" required>
        <br>
        <label for="email">Email:</label>
        <input type="email" name="email" id="email" required>
        <br>
        <div class="g-recaptcha" data-sitekey="{{ env('RECAPTCHA_SITE_KEY') }}"></div>
        <br>
        <button type="submit">Submit</button>
    </form>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

In this form, we've added a div with the g-recaptcha class to display the reCAPTCHA widget. We use the RECAPTCHA_SITE_KEY from the .env file to set the data-sitekey attribute.

Read More :- Laravel 10 Google Recaptcha V3 Validation Example Tutorial

Top comments (0)