mirror of
https://github.com/20kaushik02/express-sequelize-backend-template.git
synced 2026-01-25 07:14:06 +00:00
captcha, db check
This commit is contained in:
36
middleware/captcha.js
Normal file
36
middleware/captcha.js
Normal file
@@ -0,0 +1,36 @@
|
||||
const fetch = require("cross-fetch");
|
||||
|
||||
const typedefs = require("../typedefs");
|
||||
const logger = require("../utils/logger")(module);
|
||||
|
||||
/**
|
||||
* Google ReCAPTCHA v2 verification
|
||||
*
|
||||
* @param {typedefs.Req} req
|
||||
* @param {typedefs.Res} res
|
||||
* @param {typedefs.Next} next
|
||||
*/
|
||||
const verifyCaptcha = async (req, res, next) => {
|
||||
try {
|
||||
const secretKey = process.env.CAPTCHA_SECRET;
|
||||
|
||||
const verifyCaptchaURL = `https://google.com/recaptcha/api/siteverify?secret=${secretKey}&response=${req.body.captcha}`;
|
||||
|
||||
const captchaResp = await fetch(verifyCaptchaURL);
|
||||
const captchaData = await captchaResp.json();
|
||||
if (captchaData.success !== undefined && !captchaData.success) {
|
||||
logger.error("Recaptcha", { captchaData });
|
||||
return res.status(403).send({
|
||||
message: "Failed captcha verification"
|
||||
});
|
||||
}
|
||||
next();
|
||||
} catch (error) {
|
||||
logger.error("Error", { error });
|
||||
return res.status(500).send({ message: "Server Error. Try again." });
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
verifyCaptcha
|
||||
}
|
||||
Reference in New Issue
Block a user