-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed the GET /verify tp PATCH /verify, added email templates
- Loading branch information
OomsOoms
committed
Aug 28, 2024
1 parent
98eccda
commit d8ba3cf
Showing
11 changed files
with
382 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"jest.jestCommandLine": "npx jest --coverage" | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
While something like a content delivery network (CDN) might be used, I will just use the public folder, so any frontend assets, possibly including the css and js files will be stored within this folder. | ||
I may change this in the future but for now the assets will be stored in this folder, this might all be very wrong im not sure but it seems to make sense to me, the public folder could be replaced by an external CDN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,45 @@ | ||
const nodemailer = require('nodemailer'); | ||
const handlebars = require('handlebars'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const { logger } = require('../../config/logger'); | ||
|
||
module.exports = function (to, subject, text) { | ||
const transporter = nodemailer.createTransport({ | ||
service: 'Gmail', | ||
host: 'smtp.gmail.com', | ||
port: 465, | ||
secure: true, | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
}); | ||
const mailOptions = { | ||
from: process.env.FROM_ADDRESS, // If not set, it will be sent from the default email of the gmail account | ||
to: to, | ||
subject: subject, | ||
text: text, | ||
}; | ||
// If in development mode, do not send email | ||
if (process.env.NODE_ENV === 'development') { | ||
logger.debug('Email sent: ', mailOptions); | ||
} else { | ||
transporter.sendMail(mailOptions, (error, info) => { | ||
if (error) { | ||
logger.error('Error sending email: ', error); | ||
} else { | ||
logger.debug('Email sent: ', info.response); | ||
info.response; | ||
} | ||
}); | ||
} | ||
const transporter = nodemailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS, | ||
}, | ||
}); | ||
|
||
const COMMON_DATA = { | ||
frontendDomain: process.env.FRONTEND_DOMAIN, | ||
backendDomain: process.env.BACKEND_DOMAIN, | ||
}; | ||
|
||
async function sendEmail(to, subject, templateName, templateData) { | ||
try { | ||
const templatePath = path.join(__dirname, '../../views', `${templateName}.hbs`); | ||
|
||
const source = fs.readFileSync(templatePath, 'utf8'); | ||
const template = handlebars.compile(source); | ||
const html = template({ ...COMMON_DATA, ...templateData }); | ||
|
||
const mailOptions = { | ||
from: process.env.FROM_ADDRESS, | ||
to, | ||
subject: subject, | ||
html, | ||
}; | ||
|
||
await transporter.sendMail(mailOptions); | ||
logger.debug( | ||
`Email sent to ${to}, with the subject ${subject} and template ${templateName} and the data ${JSON.stringify(templateData)}` | ||
); | ||
} catch (error) { | ||
logger.error(error); | ||
} | ||
} | ||
|
||
module.exports = sendEmail; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.