/laravel-recaptcha-v3

recaptcha-v3 for laravel

Primary LanguagePHPMIT LicenseMIT

Google reCaptcha v3 for Laravel 5.6+

Latest Test Latest Stable Version Total Downloads GitHub license

Installation

To install, simply add it to your composer.json file:

{
    "require": {
        "huangdijia/laravel-recaptcha-v3": "^3.0"
    }
}

And run composer to update your dependencies:

composer update

Or you can simply run

composer require huangdijia/laravel-recaptcha-v3

Publish configuration file to your config folder with command:

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

Usage

Forms

// default
@recaptcha_field()

// custom
@recaptcha_field(['site_key'=>'your_key', 'name'=>'input_name'])

Init Recaptcha Javascript

Must add after @recaptcha_field()

Recaptcha v3 works best when it is loaded on every page to get the most context about interactions. Therefore, add to your header or footer template:

// default
@recaptcha_initjs()

// custom
@recaptcha_initjs(['site_key'=>'your_key', 'action' => 'action_name', 'name'=>'input_name'])

Validation as regular validation rule

Use as regular validation rule recaptcha:{ACTION},{SCORE},{HOSTNAME} like:

Validator::make($request->all(), [
    'g-recaptcha-response' => 'required|recaptcha:register,0.5,www.a.com',
    // or
    // 'g-recaptcha-response' => 'required|recaptcha',
]);

Validation as middleware

Set $routeMiddleware

    $routeMiddleware = [
        // ...
        'recaptcha' => Huangdijia\Recaptcha\Middleware\ReCaptcha::class,
    ];

Use with route

Route::get('/path')->middleware('recaptcha');
Route::get('/path')->middleware('recaptcha:{ACTION},{SCORE},{HOSTNAME}');

Hidden ReCAPTCHA

.grecaptcha-badge { 
    display: none; 
}