Skip to content

Instantly share code, notes, and snippets.

@faytekin
Last active June 2, 2022 10:59
Show Gist options
  • Save faytekin/f56201a562b8630d8f0dcbbce1b9feea to your computer and use it in GitHub Desktop.
Save faytekin/f56201a562b8630d8f0dcbbce1b9feea to your computer and use it in GitHub Desktop.
Laravel ReCaptcha v2 validation rule
<?php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Facades\Http;
class RecaptchaRule implements Rule
{
public static function make(): static
{
return new static();
}
public function passes($attribute, $value): bool
{
$response = Http::timeout(5)
->asForm()
->post('https://www.google.com/recaptcha/api/siteverify', [
'secret' => config('app.recaptcha_secret'),
'response' => $value,
'remoteip' => request()->ip(),
]);
return $response->json('success') && $response->json('score') > 0.5;
}
public function message(): string
{
return __('validation.recaptcha_error');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment