Moved contact form routes to new file

master
Keith Irwin 2017-07-04 12:04:54 -04:00
parent 7eaaf92c71
commit 7e36017bb0
No known key found for this signature in database
GPG Key ID: 378933C743E2BBC0
2 changed files with 49 additions and 49 deletions

View File

@ -9,63 +9,60 @@ const env = require('../env/env.js'),
module.exports = router
// Display contact form
.get('/contact', (req,res)=>{
.get('/', (req,res)=>{
res.render('contact', {active:'contact',
sitekey: env.recaptchaSitekey
});
})
.post('/contact', (req,res,next)=>{
.post('/', (req,res,next)=>{
// Confirm captcha
request.post( 'https://www.google.com/recaptcha/api/siteverify', {form:{
secret: env.recaptchaSecret,
response: req.body['g-recaptcha-response'],
remoteip: req.ip
}}, (err, response, body)=>{
// Confirm captcha
request.post( 'https://www.google.com/recaptcha/api/siteverify', {form:{
secret: env.recaptchaSecret,
response: req.body['g-recaptcha-response'],
remoteip: req.ip
}}, (err, response, body)=>{
// Check for errors
if (err){
mw.throwErr(err,req);
res.redirect('/contact');
}
if (response.statusCode!==200) {
let err = new Error('Bad response from reCaptcha service');
mw.throwErr(err,req);
res.redirect('/contact');
}
else {
// Check for errors
if (err){
mw.throwErr(err,req);
res.redirect('/contact');
}
if (response.statusCode!==200) {
let err = new Error('Bad response from reCaptcha service');
mw.throwErr(err,req);
res.redirect('/contact');
}
else {
// Captcha succeeded
if (JSON.parse(body).success){
mail.send({
from: `${req.body.name} <${req.body.email}>`,
to: `Tracman Contact <contact@tracman.org>`,
subject: req.body.subject||'A message',
text: req.body.message
})
.then(()=>{
req.flash('success', `Your message has been sent. `);
res.redirect(req.session.next || '/');
})
.catch((err)=>{
mw.throwErr(err,req);
res.redirect('/contact');
});
}
// Captcha failed
else {
let err = new Error('Failed reCaptcha');
// Captcha succeeded
if (JSON.parse(body).success){
mail.send({
from: `${req.body.name} <${req.body.email}>`,
to: `Tracman Contact <contact@tracman.org>`,
subject: req.body.subject||'A message',
text: req.body.message
})
.then(()=>{
req.flash('success', `Your message has been sent. `);
res.redirect(req.session.next || '/');
})
.catch((err)=>{
mw.throwErr(err,req);
res.redirect('/contact');
}
});
}
// Captcha failed
else {
let err = new Error('Failed reCaptcha');
mw.throwErr(err,req);
res.redirect('/contact');
}
}
);
//TODO: Check req.body.g-recaptcha-response
})
});
});

View File

@ -103,6 +103,9 @@ const
// Main routes
app.use( '/', require('./config/routes/index.js') );
// Contact form
app.use( '/contact', require('./config/routes/index.js') );
// Settings
app.use( '/settings', require('./config/routes/settings.js') );