Skip to content
Code and Bytes Code and Bytes

Code and Bytes
Code and Bytes

Google reCAPTCHA v2 en Laravel 12 con Livewire 3

Posted on 6 August, 202513 August, 2025

En este tutorial detallado, aprenderás a integrar Google reCAPTCHA v2 en tu aplicación Laravel 12 utilizando Livewire 3. Esta implementación ayudará a proteger tus formularios contra bots y spam, mejorando la seguridad de tu aplicación.

Configuración Inicial

1. Configura las credenciales de reCAPTCHA

Primero, necesitas registrar tu sitio en Google reCAPTCHA para obtener tus credenciales. Luego, agrégalas a tu archivo .env:

GOOGLE_RECAPTCHA2_KEY=TU_CLAVE_DE_SITIO
GOOGLE_RECAPTCHA2_SECRET=TU_CLAVE_SECRETA

2. Editar archivo config/services.php

    'recaptcha' => [
        'public_key' => env('GOOGLE_RECAPTCHA2_KEY'),
        'secret_key' => env('GOOGLE_RECAPTCHA2_SECRET'),
    ],

Implementación en Livewire

3. Edita el componente de validación

En tu componente Livewire (app/Livewire/Auth/Login.php):

    use Illuminate\Support\Facades\Http;

    #[Validate('required')]
    public $captcha = null;

    public function updatedCaptcha($token)
    {
        $response = Http::post(
            'https://www.google.com/recaptcha/api/siteverify?secret=' .
                config('services.recaptcha.secret_key') .
                '&response=' . $token
        );

        $success = $response->json()['success'];

        if (! $success) {
            throw ValidationException::withMessages([
                'captcha' => __('Google thinks, you are a bot, please refresh and try again!'),
            ]);
        } else {
            $this->captcha = true;
        }
    }

Vista Blade con reCAPTCHA

4. Implementa la vista con el widget

En tu archivo de vista (resources/views/livewire/auth/login.blade.php):

        <div id="captcha" class="mt-4" wire:ignore></div>

        @error('captcha')
        <p class="mt-3 text-sm text-red-600 text-left">
            {{ $message }}
        </p>
        @enderror
    </form>

    <script src="https://www.google.com/recaptcha/api.js?onload=handle&render=explicit" async defer>
    </script>
    <script>
        var  handle = function(e) {
            widget = grecaptcha.render('captcha', {
                'sitekey': '{{ config('services.recaptcha.public_key') }}',
                'theme': 'light', // you could switch between dark and light mode.
                'callback': verify
            });
    
        }
        var verify = function (response) {
            @this.set('captcha', response)
        }
    </script>
Laravel PHP

Post navigation

Previous post
Next post

Related Posts

Laravel

Configurar Supervisor para Queues en Laravel con Docker

Posted on 7 December, 202511 April, 2026

Si tus colas de Laravel no se procesan en Docker, aquí tienes la solución rápida. Usando Supervisor dentro de tu contenedor, podrás manejar workers de forma estable y automática. Modificaciones Necesarias 1. docker/apache/Dockerfile

Read More

PHPExcel: Uncaught exception ‘Exception’ with message ‘Could not close zip file

Posted on 18 November, 20125 April, 2014

Empezando a usar la libreria PHPExcel me encontré con el siguiente problema: 15:45:52 Create new PHPExcel object 15:45:52 Set document properties 15:45:52 Add some data 15:45:52 Rename worksheet 15:45:52 Write to Excel2007 format Fatal error: Uncaught exception ‘Exception’ with message ‘Could not close zip file /var/www/osmon/scripts/gui/hotspot/final/Tests/01simple.xlsx.’ in /var/www/osmon/scripts/gui/hotspot/final/Classes/PHPExcel/Writer/Excel2007.php:378 Stack trace: #0 /var/www/osmon/scripts/gui/hotspot/final/Tests/01simple.php(80):…

Read More
PHP

setlocale no funciona – PHP

Posted on 26 March, 2016

La función setlocale() no funciona al setearlo con el idioma deseado en este caso el español. El primer paso es consultar si se encuentra instalado en el sistema, ingresando en la terminal el comando: locale -a

Read More

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Buscar

Categorias

  • AI (2)
  • Asterisk (9)
  • Bases de datos (1)
  • DevOps (4)
  • Git (2)
  • HTML (1)
  • Laravel (6)
  • Linux (67)
  • MCP Server (1)
  • Mikrotik (3)
  • MySQL (5)
  • n8n (1)
  • Personal (4)
  • PHP (15)
  • Python (2)
  • Software recomendado (1)
  • Tips (1)
  • VSCode (4)
  • vTiger (4)
  • Windows (10)
  • Wordpress (4)
©2026 Code and Bytes | WordPress Theme by SuperbThemes