Skip to content

Commit cc38802

Browse files
authored
Merge pull request #15 from MOwsianowski/auto_refresh_token
refresh captcha token after its get expired (120s)
2 parents 1337b8b + 90a80e6 commit cc38802

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

README.md

+11-7
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ You can use some template tags to simplify the reCaptcha adoption:
6161

6262
* recaptcha_init: add the script tag for reCaptcha api. You have to put this tag somewhere in your "head" element
6363
* recaptcha_ready: call the execute function when the api script is loaded
64-
* recaptcha_execute: start the reCaptcha check and set the token from the api in your django forms
64+
* recaptcha_execute: start the reCaptcha check and set the token from the api in your django forms. Token is valid for 120s, after this time it is automatically regenerated.
6565
* recaptcha_key: if you want to use reCaptcha manually in your template, you will need the sitekey (a.k.a. public api key).
6666
This tag returns a string with the configured public key.
6767

@@ -151,12 +151,16 @@ You can use the plain javascript, just remember to set the correct value for the
151151
<script src="https://www.google.com/recaptcha/api.js?render=reCAPTCHA_site_key"></script>
152152
<script>
153153
grecaptcha.ready(function() {
154-
grecaptcha.execute('reCAPTCHA_site_key', {action: 'homepage'}).then(function(token) {
155-
document.querySelectorAll('input.django-recaptcha-hidden-field').forEach(function (value) {
156-
value.value = token;
157-
});
158-
return token;
159-
});
154+
var grecaptcha_execute = function(){
155+
grecaptcha.execute('reCAPTCHA_site_key', {action: 'homepage'}).then(function(token) {
156+
document.querySelectorAll('input.django-recaptcha-hidden-field').forEach(function (value) {
157+
value.value = token;
158+
});
159+
return token;
160+
})
161+
};
162+
grecaptcha_execute()
163+
setInterval(grecaptcha_execute, 120000);
160164
});
161165
</script>
162166
</head>

samples/bare_metal_recaptcha.html

+10-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
<script src="https://www.google.com/recaptcha/api.js?render=reCAPTCHA_site_key"></script>
44
<script>
55
grecaptcha.ready(function() {
6-
grecaptcha.execute('reCAPTCHA_site_key', {action: 'homepage'}).then(function(token) {
7-
document.querySelectorAll('input.django-recaptcha-hidden-field').forEach(function (value) {
8-
value.value = token;
9-
});
10-
return token;
11-
});
6+
var grecaptcha_execute = function(){
7+
grecaptcha.execute('reCAPTCHA_site_key', {action: 'homepage'}).then(function(token) {
8+
document.querySelectorAll('input.django-recaptcha-hidden-field').forEach(function (value) {
9+
value.value = token;
10+
});
11+
return token;
12+
})
13+
};
14+
grecaptcha_execute()
15+
setInterval(grecaptcha_execute, 120000);
1216
});
1317
</script>
1418
</head>
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
grecaptcha.execute('{{ public_key }}', {action: '{{ action_name }}'}).then(function(token) {
2-
document.querySelectorAll('input.django-recaptcha-hidden-field').forEach(function (value) {
3-
value.value = token;
4-
});
5-
return token;
6-
}){% if custom_callback %}.then(function(token) {
1+
var grecaptcha_execute = function(){
2+
grecaptcha.execute('{{ public_key }}', {action: '{{ action_name }}'}).then(function(token) {
3+
document.querySelectorAll('input.django-recaptcha-hidden-field').forEach(function (value) {
4+
value.value = token;
5+
});
6+
return token;
7+
})
8+
};
9+
grecaptcha_execute()
10+
setInterval(grecaptcha_execute, 120000);
11+
{% if custom_callback %}.then(function(token) {
712
window["{{ custom_callback }}"](token);
8-
}){% endif %}
13+
}){% endif %}

0 commit comments

Comments
 (0)