Last active
June 2, 2022 10:59
-
-
Save faytekin/f56201a562b8630d8f0dcbbce1b9feea to your computer and use it in GitHub Desktop.
Laravel ReCaptcha v2 validation rule
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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