2024-08-11 18:41:03 +03:30
|
|
|
const nodemailer = require("nodemailer");
|
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
2020-01-30 18:51:52 +03:30
|
|
|
|
2024-08-11 18:41:03 +03:30
|
|
|
const { resetMailText, verifyMailText, changeEmailText } = require("./text");
|
|
|
|
const { CustomError } = require("../utils");
|
|
|
|
const env = require("../env");
|
2019-10-08 21:56:03 +03:30
|
|
|
|
|
|
|
const mailConfig = {
|
2020-01-30 18:51:52 +03:30
|
|
|
host: env.MAIL_HOST,
|
|
|
|
port: env.MAIL_PORT,
|
|
|
|
secure: env.MAIL_SECURE,
|
2020-10-12 19:02:41 +03:30
|
|
|
auth: env.MAIL_USER
|
|
|
|
? {
|
|
|
|
user: env.MAIL_USER,
|
|
|
|
pass: env.MAIL_PASSWORD
|
|
|
|
}
|
|
|
|
: undefined
|
2019-10-08 21:56:03 +03:30
|
|
|
};
|
|
|
|
|
|
|
|
const transporter = nodemailer.createTransport(mailConfig);
|
|
|
|
|
2020-01-30 18:51:52 +03:30
|
|
|
// Read email templates
|
|
|
|
const resetEmailTemplatePath = path.join(__dirname, "template-reset.html");
|
|
|
|
const verifyEmailTemplatePath = path.join(__dirname, "template-verify.html");
|
2020-09-19 18:02:32 +04:30
|
|
|
const changeEmailTemplatePath = path.join(
|
|
|
|
__dirname,
|
|
|
|
"template-change-email.html"
|
|
|
|
);
|
2020-01-30 18:51:52 +03:30
|
|
|
const resetEmailTemplate = fs
|
|
|
|
.readFileSync(resetEmailTemplatePath, { encoding: "utf-8" })
|
2020-02-05 18:27:08 +03:30
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
.replace(/{{site_name}}/gm, env.SITE_NAME);
|
2020-01-30 18:51:52 +03:30
|
|
|
const verifyEmailTemplate = fs
|
|
|
|
.readFileSync(verifyEmailTemplatePath, { encoding: "utf-8" })
|
2020-02-05 18:27:08 +03:30
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
.replace(/{{site_name}}/gm, env.SITE_NAME);
|
2020-09-19 18:02:32 +04:30
|
|
|
const changeEmailTemplate = fs
|
|
|
|
.readFileSync(changeEmailTemplatePath, { encoding: "utf-8" })
|
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
.replace(/{{site_name}}/gm, env.SITE_NAME);
|
2020-01-30 18:51:52 +03:30
|
|
|
|
2024-08-11 18:41:03 +03:30
|
|
|
async function verification(user) {
|
2020-01-30 18:51:52 +03:30
|
|
|
const mail = await transporter.sendMail({
|
|
|
|
from: env.MAIL_FROM || env.MAIL_USER,
|
|
|
|
to: user.email,
|
|
|
|
subject: "Verify your account",
|
2020-02-05 18:27:08 +03:30
|
|
|
text: verifyMailText
|
|
|
|
.replace(/{{verification}}/gim, user.verification_token)
|
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
.replace(/{{site_name}}/gm, env.SITE_NAME),
|
|
|
|
html: verifyEmailTemplate
|
|
|
|
.replace(/{{verification}}/gim, user.verification_token)
|
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
.replace(/{{site_name}}/gm, env.SITE_NAME)
|
2020-01-30 18:51:52 +03:30
|
|
|
});
|
|
|
|
|
|
|
|
if (!mail.accepted.length) {
|
|
|
|
throw new CustomError("Couldn't send verification email. Try again later.");
|
|
|
|
}
|
2024-08-11 18:41:03 +03:30
|
|
|
}
|
2020-01-30 18:51:52 +03:30
|
|
|
|
2024-08-11 18:41:03 +03:30
|
|
|
async function changeEmail(user) {
|
2020-09-19 18:02:32 +04:30
|
|
|
const mail = await transporter.sendMail({
|
|
|
|
from: env.MAIL_FROM || env.MAIL_USER,
|
|
|
|
to: user.change_email_address,
|
|
|
|
subject: "Verify your new email address",
|
|
|
|
text: changeEmailText
|
|
|
|
.replace(/{{verification}}/gim, user.change_email_token)
|
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
.replace(/{{site_name}}/gm, env.SITE_NAME),
|
|
|
|
html: changeEmailTemplate
|
|
|
|
.replace(/{{verification}}/gim, user.change_email_token)
|
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
.replace(/{{site_name}}/gm, env.SITE_NAME)
|
|
|
|
});
|
2024-08-11 18:41:03 +03:30
|
|
|
|
2020-09-19 18:02:32 +04:30
|
|
|
if (!mail.accepted.length) {
|
|
|
|
throw new CustomError("Couldn't send verification email. Try again later.");
|
|
|
|
}
|
2024-08-11 18:41:03 +03:30
|
|
|
}
|
2020-09-19 18:02:32 +04:30
|
|
|
|
2024-08-11 18:41:03 +03:30
|
|
|
async function resetPasswordToken(user) {
|
2020-01-30 18:51:52 +03:30
|
|
|
const mail = await transporter.sendMail({
|
2020-02-05 18:27:08 +03:30
|
|
|
from: env.MAIL_FROM || env.MAIL_USER,
|
2020-01-30 18:51:52 +03:30
|
|
|
to: user.email,
|
|
|
|
subject: "Reset your password",
|
|
|
|
text: resetMailText
|
|
|
|
.replace(/{{resetpassword}}/gm, user.reset_password_token)
|
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN),
|
|
|
|
html: resetEmailTemplate
|
|
|
|
.replace(/{{resetpassword}}/gm, user.reset_password_token)
|
|
|
|
.replace(/{{domain}}/gm, env.DEFAULT_DOMAIN)
|
|
|
|
});
|
2024-08-11 18:41:03 +03:30
|
|
|
|
2020-01-30 18:51:52 +03:30
|
|
|
if (!mail.accepted.length) {
|
|
|
|
throw new CustomError(
|
|
|
|
"Couldn't send reset password email. Try again later."
|
|
|
|
);
|
|
|
|
}
|
2024-08-11 18:41:03 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
changeEmail,
|
|
|
|
verification,
|
|
|
|
resetPasswordToken,
|
|
|
|
}
|