combindma / recaptcha
Simple and painless Google reCAPTCHA V3 package for Laravel framework
Requires
- php: ^8.0
- ext-curl: *
- illuminate/contracts: ^8.0|^9.0|^10.0
- spatie/laravel-package-tools: ^1.4.3
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^6.13|^7.0|^8.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.6
README
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.