Reputation: 309
I'm trying to send the recaptcha key via AJAX, everything appears to be ok, but for some reason the php recaptcha sdk doesn't return errors, but the response is unsuccessful.
Here is my PHP code:
$recaptcha = new \ReCaptcha\ReCaptcha($user->secret);
$resp = $recaptcha->verify($data['g-recaptcha-response'], request()->ip());
if ($resp->isSuccess()) {
//Success
}else{
return response()->json([
'message' => 'Recaptcha error',
'errors' => $resp->getErrorCodes(),
], 401);
}
Ajax request:
$.ajax({
type: 'POST',
url: 'http://localhost/endpoint',
datatype: 'json',
cache: false,
data: {
email: document.getElementById('register_email').value,
password: document.getElementById('register_password').value,
password_confirmation:document.getElementById('register_re_password').value,
first_name:document.getElementById('register_first').value,
last_name:document.getElementById('register_last').value,
'g-recaptcha-response': grecaptcha.getResponse(widgetId2)
},
success: function(result) {
window.alert('Success');
grecaptcha.reset(widgetId2);
},
error: function(result) {
window.alert(result.responseJSON);
}
});
And the response:
{message: "Recaptcha error", errors: []}
The secret is working because if I put a invalid g-recaptcha-response it throws the right error, the same occurs if I change the secret.
Upvotes: 0
Views: 197
Reputation: 309
The problem was a bad site key, for some reason the SDK doesn't have an error to this problem.
If anyone have a similar problem, check that the site and secret key are complete and well copied from the ReCaptcha admin panel.
Upvotes: 0