Skip to content

Instantly share code, notes, and snippets.

@ishukshin
Created January 25, 2020 21:19
Show Gist options
  • Save ishukshin/0b3d1fda4b048428d061a728861178d1 to your computer and use it in GitHub Desktop.
Save ishukshin/0b3d1fda4b048428d061a728861178d1 to your computer and use it in GitHub Desktop.
<?php
use Request;
use Validator;
use \GuzzleHttp\Client;
// ... in the boot()
Validator::extend('recaptcha', function($attribute, $value, $parameters, $validator) {
if(!$value) {
return false;
}
$client = new Client();
$response = $client->post('https://www.google.com/recaptcha/api/siteverify', [
'form_params' => [
"secret" => env('RECAPTCHA_SECRET'),
"response" => $value,
"remoteip" => Request::ip(),
],
]);
$result = json_decode($response->getBody()->getContents(), true);
if(!isset($result['score']) or $result['score'] < 0.3) {
return false;
}
return true;
});
<script src="https://www.google.com/recaptcha/api.js?render={{ env('RECAPTCHA_KEY') }}"></script>
<script>
grecaptcha.ready(function() {
grecaptcha.execute("{{ env('RECAPTCHA_KEY') }}", {action: 'login'}).then(function(token) {
$('#login-recaptcha').html('<input type=hidden name="g-recaptcha-response" value="' + token + '">')
});
});
</script>
<div class="line">
<div class="form-group">
<div id="login-recaptcha" class="g-recaptcha" style=""></div>
</div>
@if ($errors->has('g-recaptcha-response'))
<label>{{ trans('auth.reca') }}</label>
@endif
</div>
<?php
protected function validateLogin(Request $request)
{
$params = [
$this->username() => 'required|string',
'password' => 'required|string',
];
// appending validator
if($request->input('fixrecap') !== env('FIXRECAP')) {
$params['g-recaptcha-response'] = 'recaptcha';
}
# validate
$validator = $this->getValidationFactory()->make($request->all(), $params, [], []);
if ($validator->fails()) {
// ...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment