Laravel Captcha

website·Indexed 30 days ago

https://laravel-captcha.rahuldey.dev

A package for integrating various CAPTCHA options into Laravel applications. Version 2.x requires PHP 8.0+ and provides components like x-captcha-js, x-captcha-button, and x-captcha-container, along with built-in validation mechanisms, custom driver support, and a Captcha::fake() method for testing.

Tokens
2.7K
Snippets
28
Records
29
Agent score
50%

What's inside Laravel Captcha

  1. Overview of Laravel Captcha package

    Laravel Captcha is an open-source package designed to integrate various captcha providers into Laravel applications to provide security against bots and spambots. It supports the following providers: HCaptcha, reCAPTCHA, and Turnstile.
  2. Implement invisible captcha with Turnstile service

    When using the Turnstile Captcha service, the x-captcha-button component is not required because Turnstile handles the invisible captcha configuration internally. Instead, use the x-captcha-container component within your form after setting the invisible captcha key.
    <form action="" method="post">
        ... 
        <x-captcha-container />
    </form>
  3. Simulate successful captcha validation in tests

    Use the Captcha::fake() method to simulate a captcha response that always passes. This allows you to test form submission logic and validation without manually solving captchas during automated tests.
    use Rahul900day
    Captcha\Facades\Captcha;
    use Rahul900day\Captcha\Rules\Captcha as CaptchaRule;
    use Illuminate\Support\Facades\Validator;
    
    // Simulate a successful captcha response
    Captcha::fake();
    
    $validation = Validator::make($request, [
       Captcha::getResponseName() => [
           'required', 
            new CaptchaRule(),
       ],
    ]);
    
    // This assertion will pass because the captcha is faked as successful
    $this->assertTrue(! $validation->fails());
  4. Upgrade Laravel Captcha from 1.x to 2.0

    To upgrade Laravel Captcha to version 2.0, ensure your environment meets the minimum requirements: PHP v8.0 and Laravel v8.0.0. Update the package dependency in your composer.json file from ^1.2 to ^2.0.
    "rahul900day/laravel-captcha": "^2.0"
  5. Validate the Laravel Captcha response

    Validate the Captcha response using Laravel's Validator facade. Use Captcha::getResponseName() to dynamically retrieve the input field name and apply the CaptchaRule to ensure the response is valid.
    use Rahul900day\Captcha\Facades\Captcha;
    use Rahul900day\Captcha\Rules\Captcha as CaptchaRule;
    use Illuminate\Support\Facades\Validator;
    
    Validator::make($request, [
        Captcha::getResponseName() => [
            'required', 
            new CaptchaRule(),
        ],
    ]);
  6. Create and register a custom Captcha driver

    To incorporate a custom captcha service or driver in Laravel Captcha, use the Captcha::extend method. This registration must occur within the boot method of the AppServiceProvider.php file. The custom driver class must implement the Rahul900day achten//Contracts\Captcha interface to ensure consistent behavior with the package.
    use Rahul900day
    Captcha
    Facades
    Captcha;
    use App
    Captcha
    CustomCaptchaDriver;
    
    Captcha::extend('custom-driver', function($app) {
        return new CustomCaptchaDriver($app);
    });
  7. Validate captcha responses using Laravel validation

    You can validate a captcha response using either the CaptchaRule class or the 'captcha' validation alias. The validation should be applied to the field name returned by Captcha::getResponseName().
    use Rahul900day
    Captcha
    Facades
    Captcha;
    use Rahul900day
    Captcha
    Rules
    Captcha as CaptchaRule;
    use Illuminate
    Support
    Facades
    Validator;
    
    // Option 1: Using the CaptchaRule class
    $validator = Validator::make($request->all(), [
        Captcha::getResponseName() => [
            'required',
            new CaptchaRule(),
        ]
    ]);
    
    // Option 2: Using the 'captcha' validation alias
    $validator = Validator::make($request->all(), [
        Captcha::getResponseName() => 'required|captcha'
    ]);
  8. Simulate failed captcha validation in tests

    To test how your application handles a failed captcha attempt, pass false to the Captcha::fake() method. This forces the captcha validation rule to always fail, regardless of the input provided in the request.
    // Simulate a captcha failure
    Captcha::fake(false);
  9. Inject Captcha JavaScript using the x-captcha-js component

    The x-captcha-js component injects the required JavaScript into your application to enable Captcha services such as hCaptcha, reCAPTCHA, or Turnstile. For the scripts to load correctly, this component should be placed within the <head> tag of your HTML layout.
    <head>
        <x-captcha-js />
    </head>
  10. Localize captcha validation error messages

    If your application supports multiple languages, you can define the captcha error message in your Laravel language files located in resources/lang/{locale}/validation.php. Laravel will automatically select the message based on the current application locale.
    // resources/lang/es/validation.php
    
    return [
        // Other validation messages...
        'Your Custom Captcha Error Message' => 'Su mensaje de error de Captcha personalizado',
    ];