2020-06-29 10:09:10 +01:00
var express = require ( 'express' ) ;
2020-07-01 12:10:30 +01:00
var rate _limit = require ( "express-rate-limit" )
2020-06-29 10:09:10 +01:00
const { verify } = require ( 'hcaptcha' ) ;
const nodemailer = require ( 'nodemailer' )
var router = express . Router ( ) ;
2020-07-01 12:10:30 +01:00
const contact _rate _limit = rate _limit ( {
windowMs : 10 * 60 * 1000 , // 10 minutes
max : 1 , // 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-06-29 10:09:10 +01:00
// POST route from contact form
2020-07-01 12:10:30 +01:00
router . post ( '/' , contact _rate _limit , ( req , res ) => {
2020-06-29 10:09:10 +01:00
const TO _GMAIL _USER = process . env . TO _GMAIL _USER
const FROM _GMAIL _USER = process . env . FROM _GMAIL _USER
const GMAIL _PASS = process . env . GMAIL _PASS
const HCAPTCHA _KEY = process . env . HCAPTCHA _KEY
const token = req . body [ "g-recaptcha-response" ] ;
2020-07-01 12:10:30 +01:00
const ip = req . headers [ 'x-forwarded-for' ] || req . connection . remoteAddress ;
2020-06-29 10:09:10 +01:00
verify ( HCAPTCHA _KEY , token )
. then ( ( data ) => {
if ( data . success === true ) {
// Instantiate the SMTP server
const smtpTrans = nodemailer . createTransport ( {
host : 'smtp.gmail.com' ,
port : 465 ,
secure : true ,
auth : {
user : FROM _GMAIL _USER ,
pass : GMAIL _PASS
}
} )
// Specify what the email will look like
const mailOpts = {
from : 'Pastel.codes Contact Notifications' , // This is ignored by Gmail
to : TO _GMAIL _USER ,
subject : 'New message from contact form at pastel.codes' ,
2020-07-01 12:10:30 +01:00
text : ` ${ req . body . firstname } ${ req . body . lastname } ( ${ req . body . email } ) \n says: ${ req . body . message } \n \n ip: ${ ip } `
2020-06-29 10:09:10 +01:00
}
// maybe send conformation email?
// Attempt to send the email
smtpTrans . sendMail ( mailOpts , ( error , response ) => {
if ( error ) {
console . log ( error )
2020-07-01 12:10:30 +01:00
res . render ( 'error' , { message : "Email did not send" } ) // Show a page indicating failure
2020-06-29 10:09:10 +01:00
}
else {
2020-07-01 12:10:30 +01:00
res . render ( 'contact' , { message : "I will get back to you soon!" , success : "Make sure the email is from " , email : TO _GMAIL _USER } ) // Show a page indicating success
2020-06-29 10:09:10 +01:00
}
} )
} else {
// rerender with same info in the text box and show error message
2020-07-01 12:10:30 +01:00
res . render ( 'contact' , { message : "Captcha failed, try again" } ) ;
2020-06-29 10:09:10 +01:00
}
} )
. catch ( error => {
console . log ( error ) ;
2020-07-01 12:10:30 +01:00
res . render ( 'contact' , { message : "Something wrong happened, try again later" } ) ;
2020-06-29 10:09:10 +01:00
} ) ;
} )
/* GET home page. */
router . get ( '/' , function ( req , res , next ) {
2020-07-01 12:10:30 +01:00
res . render ( 'contact' , { title : 'Contact' , description : "Contact me!" } ) ;
2020-06-29 10:09:10 +01:00
} ) ;
module . exports = router ;