2020-06-29 10:09:10 +01:00
|
|
|
var express = require('express');
|
2024-04-29 13:21:31 +01:00
|
|
|
var rate_limit = require('express-rate-limit');
|
|
|
|
const { verify } = require('hcaptcha');
|
2020-06-29 10:09:10 +01:00
|
|
|
var router = express.Router();
|
|
|
|
|
2020-07-01 15:00:52 +01:00
|
|
|
const sgMail = require('@sendgrid/mail');
|
|
|
|
sgMail.setApiKey(process.env.SENDGRID_API_KEY);
|
|
|
|
|
2020-07-01 12:10:30 +01:00
|
|
|
const contact_rate_limit = rate_limit({
|
2024-04-29 13:21:31 +01:00
|
|
|
windowMs: 10 * 60 * 1000, // 10 minutes
|
|
|
|
max: 5, // limit each IP to 10 requests per windowMs
|
|
|
|
message: 'Too many contact requests, try again later.',
|
|
|
|
handler: function (req, res /*, next*/) {
|
|
|
|
res.render('error', {
|
|
|
|
title: 'Error',
|
|
|
|
message: 'Too many contact requests, try again later.',
|
|
|
|
error: { status: null },
|
|
|
|
});
|
|
|
|
},
|
2020-07-01 12:10:30 +01:00
|
|
|
});
|
|
|
|
|
2020-06-29 10:09:10 +01:00
|
|
|
// POST route from contact form
|
2020-07-01 15:00:52 +01:00
|
|
|
router.post('/', contact_rate_limit, (req, res) => {
|
2024-04-29 13:21:31 +01:00
|
|
|
const TO_MAIL_USER = process.env.TO_MAIL_USER;
|
|
|
|
const FROM_MAIL_USER = process.env.FROM_MAIL_USER;
|
|
|
|
const HCAPTCHA_KEY = process.env.HCAPTCHA_KEY;
|
|
|
|
const REPLY_TO_MAIL = process.env.REPLY_TO_MAIL;
|
|
|
|
const token = req.body['g-recaptcha-response'];
|
|
|
|
const ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
|
2020-06-29 10:09:10 +01:00
|
|
|
|
2024-04-29 13:21:31 +01:00
|
|
|
verify(HCAPTCHA_KEY, token)
|
|
|
|
.then((data) => {
|
|
|
|
if (data.success === true) {
|
|
|
|
const msg = {
|
|
|
|
to: TO_MAIL_USER,
|
|
|
|
from: FROM_MAIL_USER,
|
|
|
|
subject: 'New message from contact form at pastel.codes',
|
|
|
|
text: `${req.body.firstname} ${req.body.lastname} (${req.body.email})\nsays: ${req.body.message}\n\nip: ${ip}`,
|
|
|
|
};
|
2020-06-29 10:09:10 +01:00
|
|
|
|
2024-04-29 13:21:31 +01:00
|
|
|
sgMail
|
|
|
|
.send(msg)
|
|
|
|
.then(() => {
|
|
|
|
res.render('contact', {
|
|
|
|
title: 'Contact',
|
|
|
|
message: 'I will get back to you soon!',
|
|
|
|
success: 'Make sure the email is from ',
|
|
|
|
email: REPLY_TO_MAIL,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
2020-06-29 10:09:10 +01:00
|
|
|
console.log(error);
|
2024-04-29 13:21:31 +01:00
|
|
|
res.render('error', {
|
|
|
|
title: 'Contact',
|
|
|
|
message: 'Email did not send',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// rerender with same info in the text box and show error message
|
|
|
|
res.render('contact', {
|
|
|
|
title: 'Contact',
|
|
|
|
message: 'Captcha failed, try again',
|
2020-06-29 10:09:10 +01:00
|
|
|
});
|
2024-04-29 13:21:31 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
res.render('contact', {
|
|
|
|
title: 'Contact',
|
|
|
|
message: 'Something wrong happened, try again later',
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-06-29 10:09:10 +01:00
|
|
|
|
|
|
|
/* GET home page. */
|
2024-04-29 13:21:31 +01:00
|
|
|
router.get('/', function (req, res, _next) {
|
|
|
|
res.render('contact', { title: 'Contact', description: 'Contact me!' });
|
2020-06-29 10:09:10 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|