|
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 | +}; |
2 | 53 |
|
3 | 54 | module.exports = { |
4 | 55 | extend: '@apostrophecms/form', |
5 | 56 | handlers(self) { |
6 | 57 | 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), |
46 | 59 | }; |
47 | | - } |
| 60 | + }, |
48 | 61 | }; |
0 commit comments