Skip to content
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

FIO-7996: refactor recaptcha validation #5528

Merged
merged 2 commits into from
Mar 5, 2024
Merged
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
69 changes: 29 additions & 40 deletions src/components/recaptcha/ReCaptcha.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default class ReCaptchaComponent extends Component {
return true;
}

verify(actionName) {
async verify(actionName) {
const siteKey = _get(this.root.form, 'settings.recaptcha.siteKey');
if (!siteKey) {
console.warn('There is no Site Key specified in settings in form JSON');
Expand All @@ -73,40 +73,34 @@ export default class ReCaptchaComponent extends Component {
const recaptchaApiScriptUrl = `https://www.google.com/recaptcha/api.js?render=${_get(this.root.form, 'settings.recaptcha.siteKey')}`;
this.recaptchaApiReady = Formio.requireLibrary('googleRecaptcha', 'grecaptcha', recaptchaApiScriptUrl, true);
}
if (this.recaptchaApiReady) {
try {
await this.recaptchaApiReady;
this.recaptchaVerifiedPromise = new Promise((resolve, reject) => {
this.recaptchaApiReady
.then(() => {
if (!this.isLoading) {
this.isLoading= true;
grecaptcha.ready(_debounce(() => {
grecaptcha
.execute(siteKey, {
action: actionName
})
.then((token) => {
return this.sendVerificationRequest(token).then(({ verificationResult, token }) => {
this.recaptchaResult = {
...verificationResult,
token,
};
this.updateValue(this.recaptchaResult);
return resolve(verificationResult);
});
})
.catch(() => {
this.isLoading = false;
});
}, 1000));
if (!this.isLoading) {
this.isLoading= true;
grecaptcha.ready(_debounce(async() => {
try {
const token = await grecaptcha.execute(siteKey, { action: actionName });
const verificationResult = await this.sendVerificationRequest(token);
this.recaptchaResult = {
...verificationResult,
token,
};
this.updateValue(this.recaptchaResult);
this.isLoading = false;
return resolve(verificationResult);
}
})
.catch(() => {
return reject();
});
}).then(() => {
this.isLoading = false;
catch (err) {
this.isLoading = false;
reject(err);
}
}, 1000));
}
});
}
catch (err) {
this.loading = false;
}
}

beforeSubmit() {
Expand All @@ -118,16 +112,15 @@ export default class ReCaptchaComponent extends Component {
}

sendVerificationRequest(token) {
return Formio.makeStaticRequest(`${Formio.projectUrl}/recaptcha?recaptchaToken=${token}`)
.then((verificationResult) => ({ verificationResult, token }));
return Formio.makeStaticRequest(`${Formio.projectUrl}/recaptcha?recaptchaToken=${token}`);
}

checkComponentValidity(data, dirty, row, options = {}) {
data = data || this.rootValue;
row = row || this.data;
const { async = false } = options;

// Verification could be async only
// Verification could be async only (which for now is only the case for server-side validation)
if (!async) {
return super.checkComponentValidity(data, dirty, row, options);
}
Expand All @@ -143,12 +136,8 @@ export default class ReCaptchaComponent extends Component {
return Promise.resolve(false);
}

return this.hook('validateReCaptcha', componentData.token, () => Promise.resolve(true))
.then((success) => success)
.catch((err) => {
this.setCustomValidity(this.t(err.message || err));
return false;
});
// Any further validation will 100% not run on the client
return Promise.resolve(true);
}

normalizeValue(newValue) {
Expand Down
Loading