Created
January 25, 2020 21:19
-
-
Save ishukshin/0b3d1fda4b048428d061a728861178d1 to your computer and use it in GitHub Desktop.
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 | |
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; | |
}); |
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
<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> |
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
<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> |
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 | |
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