-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
32 lines (28 loc) · 878 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const hcaptcha = require('hcaptcha');
// validate takes an hCaptcha secret and returns
// an express middleware function
const validate = (secret) => (req, res, next) => {
// get token from the body
const token = req.body['h-captcha-response'];
// call next with an error if no token present
if (!token) {
return res.status(400).json({ success: false, message: 'No token response' });
}
// console.log(token)
// verify the hcaptcha and continue on success
// call next with an error if verification errors or fails
return hcaptcha.verify(secret, token)
.then((data) => {
req.hcaptcha = data;
if(req.hcaptcha.success===true){
req.hcaptcha.message="hCaptcha verified"
}else{
req.hcaptcha.message="hCaptcha not verified"
}
next()
})
.catch(next);
};
module.exports.middleware = {
validate,
};