-
composer.json
Open in GitHub{ // "require": { "php": "^7.3|^8.0", // "google/recaptcha": "^1.2", }, // }
-
app/Rules/RecaptchaRule.php
Open in GitHubuse Illuminate\Contracts\Validation\Rule; use ReCaptcha\ReCaptcha; class RecaptchaRule implements Rule { private $errors; public function passes($attribute, $value) { if (config('recaptcha.testing') || config('recaptcha.testing') !== null) { return true; } $rScore = 0.5; if (empty($value)) { $this->errors = ':atrribute field is required'; return false; } $recaptcha = new Recaptcha(config('recaptcha.secret_key')); $resp = $recaptcha->setExpectedHostname($_SERVER['HTTP_HOST']) ->setScoreThreshold($rScore) ->verify($value, $_SERVER['REMOTE_ADDR']); if (!$resp->isSuccess()) { $this->errors = $resp->getErrorCodes(); return false; } if ($resp->getScore() < $rScore) { $this->errors = 'Failed to valildate ReCaptcha'; return false; } return true; } public function message() { return $this->errors; } }
-
app/Http/Requests/AuthLoginRequest.php
Open in GitHubuse Illuminate\Foundation\Http\FormRequest; use App\Rules\RecaptchaRule; class AuthLoginRequest extends FormRequest { // public function rules() { return [ 'email' => 'required', 'password' => 'required', 'recaptcha_token' => ['required', new RecaptchaRule($this->recaptcha_token) ], ]; } // }