Reputation: 119
How to increase the google ReCaptcha expiration time? I have tried below one,
<script>
var callback = function() {
grecaptcha.render('id-of-render-element', {
'sitekey': 'your-site-key',
'expired-callback': expCallback
});
};
var expCallback = function() {
grecaptcha.reset();
};
</script>
<div id="id-of-render-element"></div>
<script src="https://www.google.com/recaptcha/api.js?onload=callback&render=explicit" async defer></script>
But it is not working.
Upvotes: 7
Views: 22359
Reputation:
Best Solution
<input type='hidden' name='recaptcha_token' id='recaptcha_token'>
<script src="https://www.google.com/recaptcha/api.js?render={{ env("GOOGLE_CAPTCHA_PUBLIC_KEY") }}"></script>
<script>
setInterval(function () {
grecaptcha.ready(function() {
grecaptcha.execute('{{ env("GOOGLE_CAPTCHA_PUBLIC_KEY") }}')
.then(function(token) {
document.getElementById("recaptcha_token").value = token;
});
});
}, 30 * 1000);
</script>
Upvotes: 1
Reputation: 1237
This has worked for me on both Invisible and the Checkbox versions. I understand you want to "increase" the time, however that is not an option Google offers. I simply reboot the box every 5 min.
setInterval(function(){ grecaptcha.reset(); }, 5 * 60 * 1000 );
// just reset every 5 min
Also, setting the "Advanced" box slider in your Google Developer Console to "least restrictions" seems to help.
Keep in mind with the new "Invisible" Recapcha you may not need to reset or extend the time unless you have additional AJAX validation. I have noticed that when I use the new "invisible" method that the box stays good forever if I only trigger it when the form is validated first.
Q.E.D. A better solution to the problem might then be simple to use this https://developers.google.com/recaptcha/docs/invisible#auto_render
Upvotes: 11