Skip to content

feat: using google's recaptcha3 to perform a bot detection #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions _includes/scripts.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@
type="text/javascript"
src="{{ site.base-url }}/js/form-submission-handler.js"
></script>

<!-- recaptcha API with client ID -->
<script src="https://www.google.com/recaptcha/api.js?render=6LdEaA4qAAAAACDNhU72_De3d9dNhGJ87dKyuFby"></script>
13 changes: 12 additions & 1 deletion js/form-submission-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
return {data: formData, honeypot: honeypot};
}

function handleFormSubmit(event) { // handles form submit without any jquery
async function handleFormSubmit(event) { // handles form submit without any jquery
event.preventDefault(); // we are submitting via xhr below
var form = event.target;
var formData = getFormData(form);
Expand All @@ -60,6 +60,17 @@
if (formData.honeypot) {
return false;
}

/* BOTS DETECTION USING RECAPTCHA3 */
const clientId = '6LdEaA4qAAAAACDNhU72_De3d9dNhGJ87dKyuFby';
const token = await window.grecaptcha.execute(clientId, { action: 'submit' }); // get token from recatpcha frontend client
const captchaResult = await (await fetch(`https://script.google.com/macros/s/AKfycbxRKmuN_AoP_4gzvsio4L101YCcI_1n1XUWB1YMgHKqhZDSEeayxJXo7wRks24PMKhe/exec?token=${token}`)).json(); // retrieve result from recaptcha's backend script
const { score } = captchaResult;
// score is a float between 0 and 1 (1 = certainly a human; 0 = certainly a robot)
if (score < 0.5) {
alert('BEGONE, BOT !');
return false;
}

disableAllButtons(form);
var url = form.action;
Expand Down