combindma/recaptcha

Simple and painless Google reCAPTCHA V3 package for Laravel framework

2.1.0 2023-03-18 16:11 UTC

This package is auto-updated.

Last update: 2024-12-18 20:12:26 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Simple and painless Google reCAPTCHA V3 package for Laravel framework.

Inspired by: https://github.com/biscolab/laravel-recaptcha

Installation

You can install the package via composer:

composer require combindma/recaptcha

You can publish the config file with:

php artisan vendor:publish --provider="Combindma\Recaptcha\RecaptchaServiceProvider" --tag="recaptcha-config"

This is the contents of the published config file:

return [

    //The name of the input used to send Google reCAPTCHA token to verify
    'token_name' => env('RECAPTCHA_NAME', 'recaptcha_token'),

    /**
     *
     * The site key
     * get site key @ www.google.com/recaptcha/admin
     *
     */
    'api_site_key' => env('RECAPTCHA_SITE_KEY', ''),

    /**
     *
     * The secret key
     * get secret key @ www.google.com/recaptcha/admin
     *
     */
    'api_secret_key' => env('RECAPTCHA_SECRET_KEY', ''),

    /**
     *
     * The curl timout in seconds to validate a recaptcha token
     *
     */
    'curl_timeout' => 10,

    /**
     *
     * Set API domain. You can use "www.recaptcha.net" in case "www.google.com" is not accessible.
     * (no check will be made on the entered value)
     * @see   https://developers.google.com/recaptcha/docs/faq#can-i-use-recaptcha-globally
     * @since v4.3.0
     * Default 'www.google.com' (ReCaptchaBuilder::DEFAULT_RECAPTCHA_API_DOMAIN)
     *
     */
    'api_domain' => 'www.google.com',

    //you can disable the ReCaptcha when you test your application
    'enabled' => env('RECAPTCHA_ENABLED', 'true'),
    
];

You can publish the translations file with:

php artisan vendor:publish --provider="Combindma\Recaptcha\RecaptchaServiceProvider" --tag="recaptcha-translations"

This is the contents of the published translations file:

return [
    'invalid' => 'We could not verify if you are a robot or not. Please refresh the page.',
];

Usage

Embed in Blade

Insert htmlScriptTagJsApi($config) helper before closing tag.

<!DOCTYPE html>
<html>
<head>
    {!! htmlScriptTagJsApi(['action' => 'homepage']) !!}
</head>

Insert recaptchaInput() helper inside your form tag.

<form action="" method="POST">
    @csrf
    {!! recaptchaInput() !!}
    <!-- More inputs -->
</form>

Validate the token

Add the rule 'recaptcha' in your validation rules request or in your controller

 
 $request->validate([
       //... other rules
       
       //Add this to your validation rule
       config('recaptcha.token_name') => ['required','string', 'recaptcha']
       ]);

If the validation fails, an error will be returned.

Testing

composer test

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.