Skip to content

Commit 431b416

Browse files
committed
Fix lint errors
1 parent 2d92e5d commit 431b416

4 files changed

Lines changed: 405 additions & 75 deletions

File tree

website/modules/@apostrophecms/form-widget/views/widget.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
{% area form, 'contents' %}
2424

2525
{% if recaptchaReady %}
26+
{# sonar-ignore-start #}
2627
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
28+
{# sonar-ignore-end #}
2729
<div class="g-recaptcha"
2830
data-sitekey="{{ recaptchaSite }}">
2931
</div>
Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,61 @@
1-
const fetch = require('node-fetch');
1+
const fetch = require('node-fetch');
2+
3+
const handleSubmit = async (self, req) => {
4+
const global = await req.apos.global.get(req);
5+
const recaptchaToken = req.body['g-recaptcha-response'];
6+
7+
const enableRecaptcha = global.useRecaptcha && global.recaptchaSecret;
8+
9+
if (enableRecaptcha) {
10+
if (!recaptchaToken) {
11+
return req.res.status(400).json({ error: 'Missing reCAPTCHA token.' });
12+
}
13+
14+
try {
15+
const params = new URLSearchParams();
16+
params.append('secret', global.recaptchaSecret);
17+
params.append('response', recaptchaToken);
18+
params.append('remoteip', req.ip);
19+
20+
const response = await fetch(
21+
'https://www.google.com/recaptcha/api/siteverify',
22+
{
23+
method: 'POST',
24+
headers: {
25+
'Content-Type': 'application/x-www-form-urlencoded',
26+
},
27+
body: params,
28+
timeout: 5000,
29+
},
30+
);
31+
if (!response.ok) {
32+
throw new Error(`HTTP error! status: ${response.status}`);
33+
}
34+
35+
const data = await response.json();
36+
37+
if (!data.success) {
38+
return req.res
39+
.status(400)
40+
.json({ error: 'reCAPTCHA verification failed.' });
41+
}
42+
} catch (err) {
43+
// Logging the error for debugging purposes
44+
/* eslint-disable-next-line no-console */
45+
console.error('reCAPTCHA error:', err);
46+
47+
return req.res.status(500).json({ error: 'Error verifying reCAPTCHA.' });
48+
}
49+
}
50+
51+
return self.super.handlers.submit(req);
52+
};
253

354
module.exports = {
455
extend: '@apostrophecms/form',
556
handlers(self) {
657
return {
7-
async submit(req) {
8-
const global = await req.apos.global.get(req);
9-
const recaptchaToken = req.body['g-recaptcha-response'];
10-
11-
const enableRecaptcha = global.useRecaptcha && global.recaptchaSecret;
12-
13-
if (enableRecaptcha) {
14-
if (!recaptchaToken) {
15-
return req.res.status(400).json({ error: 'Missing reCAPTCHA token.' });
16-
}
17-
18-
try {
19-
const params = new URLSearchParams();
20-
params.append('secret', global.recaptchaSecret);
21-
params.append('response', recaptchaToken);
22-
params.append('remoteip', req.ip);
23-
24-
const response = await fetch(
25-
'https://www.google.com/recaptcha/api/siteverify',
26-
{
27-
method: 'POST',
28-
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
29-
body: params
30-
}
31-
);
32-
33-
const data = await response.json();
34-
35-
if (!data.success) {
36-
return req.res.status(400).json({ error: 'reCAPTCHA verification failed.' });
37-
}
38-
} catch (err) {
39-
return req.res.status(500).json({ error: 'Error verifying reCAPTCHA.' });
40-
}
41-
}
42-
43-
// Pass control to the original Apostrophe form submit handler
44-
return self.super.handlers.submit(req);
45-
}
58+
submit: (req) => handleSubmit(self, req),
4659
};
47-
}
60+
},
4861
};

0 commit comments

Comments
 (0)