mostafaznv/recaptcha

Laravel package for Google Recaptcha v3

1.1.4 2021-02-25 16:12 UTC

This package is auto-updated.

Last update: 2024-11-07 21:35:20 UTC


README

Laravel package for Google Recaptcha v3

Some of the features for Recaptcha:

  • Configurable and Customizable
  • Callback function
  • Service Container
  • Auto discovery (only laravel 5.5+)

Requirements:

  • Laravel 5.3 or higher
  • PHP 5.6.4 or higher

Installation

  1. Install the package via composer:
    composer require mostafaznv/recaptcha
  2. Register Provider and Facade in config/app.php (Required for Laravel 5.3 and 5.4):
    'providers' => [
      ...
      Mostafaznv\Recaptcha\RecaptchaServiceProvider::class,
    ],
    
    
    'aliases' => [
      ...
      'Recaptcha' => Mostafaznv\Recaptcha\Facades\Recaptcha::class,
    ]
  3. Publish config and views:
    php artisan vendor:publish --provider="Mostafaznv\Recaptcha\RecaptchaServiceProvider"
  4. Done

Configuration

Add RECAPTCHA_SITE_KEY and RECAPTCHA_SECRET_KEY to .env file:

RECAPTCHA_SITE_KEY=your-site-key
RECAPTCHA_SECRET_KEY=your-secret-key

to set more, open config/recaptcha.php and set your own configurations.

return [
    'secret_key' => env('RECAPTCHA_SECRET_KEY'),
    'site_key'   => env('RECAPTCHA_SITE_KEY'),

    'is_active' => true,

    'score' => 0.5,

    'options' => [
        'timeout' => 5.0,
    ]
];

Usage

  1. Render JS
    {!! Recaptcha::renderJs() !!}
  2. Create Recaptcha field

    this is a hidden input field that gets filled with a recaptcha token.

    {!! Recaptcha::field('login') !!}
  3. Validate
    $validate = Validator::make(Input::all(), [
       'g-recaptcha-response' => 'required|recaptcha:login'
    ]);
  4. Done

Custom Validation Message

Add the following values to the custom array in the validation language file:

'custom' => [     
    'g-recaptcha-response' => [
        'required' => 'Please verify that you are not a robot.',
        'recaptcha' => 'Captcha error! try again later or contact site admin.',
    ],
],

Then check for captcha errors in the Form:

@if ($errors->has('g-recaptcha-response'))
    <p>{{ $errors->first('g-recaptcha-response') }}</p>
@endif

Validate Score Manually

Alternatively, you can get the score and take variable action:

$token = $request->get('g-recaptcha-response');
$action = 'home';
$score = 0;

$score = app('recaptcha')->verify($token, $action, $score);

if($score > 0.7) {
    // is valid
} 
else if($score > 0.3) {
    // require additional email verification
} 
else {
    return abort(400, 'Bad request');
}

Methods

RenderJs

Field

Validation

Complete Example

View:
<html>
    <head>
        <title>Recaptcha v3</title>
        
        {!! Recaptcha::renderJs('fa') !!}
    </head>

    <body>
        <form action="{{ url('recaptcha-page') }}" method="post">
            {!! csrf_field() !!}
            {!! Recaptcha::field('home', 'g-recaptcha-response', ['id' => 'recaptcha-id', 'class' => 'form-element'], 'recaptchaCallback') !!}
        
            <button type="submit">Submit</button>
        </form>
        
        @if($errors->any())
            @foreach($errors->all() as $key => $error)
                <p>{{ $key }} - {{ $error }}</p>
            @endforeach
        @endif
        
        
        <script>
            function recaptchaCallback(token) {
                console.log('token retrieved:');
                console.log(token);
            }
        </script>
    </body>
</html>
Controller:
class DevController extends Controller
{
    public function verifyRecaptcha(Request $request)
    {
        $this->validate($request, ['g-recaptcha-response' => 'required|recaptcha:home,0.5']);
        dd($request->all());
    }
}

Migration Guide

from 1.0.0 to 1.1.0
  • delete app/recaptcha.php file
  • delete resources/views/vendor/recaptcha directory
  • publish vendor again
    php artisan vendor:publish --provider="Mostafaznv\Recaptcha\RecaptchaServiceProvider"

Changelog

Refer to the Changelog for a full history of the project.

License

This software is released under The MIT License (MIT).

(c) 2018 Mostafaznv, All rights reserved.