Google Re Captcha
Summary
Laravel - v2 recaptcha
https://medium.com/@donjadene/integrate-google-recaptcha-v2-in-laravel-ca4d2460afc8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?php
namespace App\\Rules;
use Closure;
use Illuminate\\Contracts\\Validation\\ValidationRule;
use Illuminate\\Support\\Facades\\Http;
class Recaptcha implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \\Closure(string): \\Illuminate\\Translation\\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
$gResponseToken = (string) $value;
$response = Http::asForm()->post(
'<https://www.google.com/recaptcha/api/siteverify>',
['secret' => env('RECAPTCHA_SECRET_KEY'), 'response' => $gResponseToken]
);
if (!json_decode($response->body(), true)['success']) {
$fail('Invalid recaptcha');
}
}
}
'g-recaptcha-response' => ['required', new Recaptcha],