1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function recaptcha(settings = null)
|
16 | {
|
17 | if (settings)
|
18 | {
|
19 |
|
20 | const privateKey = settings.privateKey || false;
|
21 | const publicKey = settings.publicKey || false;
|
22 |
|
23 | if (privateKey && publicKey)
|
24 | {
|
25 | let Recaptcha = require('node-recaptcha2')
|
26 | .Recaptcha;
|
27 |
|
28 |
|
29 | return function (req, res, next)
|
30 | {
|
31 | if (!req.body.recaptchaResponse || typeof req.body.recaptchaResponse !== 'string' || req.body.recaptchaResponse.length < 1)
|
32 | {
|
33 | return res.error('reCAPTCHA response not included');
|
34 | }
|
35 | (new Recaptcha(publicKey, privateKey, {
|
36 | remoteip: req.clientIp,
|
37 | response: req.body.recaptchaResponse
|
38 | }))
|
39 | .verify((success, errCode) =>
|
40 | {
|
41 | if (!success)
|
42 | {
|
43 | res.error('Failed verifying reCAPTCHA');
|
44 | }
|
45 | else
|
46 | {
|
47 | next();
|
48 | }
|
49 | });
|
50 | };
|
51 | }
|
52 | }
|
53 |
|
54 | return function (req, res, next)
|
55 | {
|
56 | next();
|
57 | };
|
58 | }
|
59 |
|
60 | module.exports = recaptcha;
|