Update SSO mail sending
This commit is contained in:
parent
e6496ee2cf
commit
ac9e9b468a
4
engine/user/one-time-code.html
Normal file
4
engine/user/one-time-code.html
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<h1>One Time Code</h1>
|
||||||
|
<p>Please use this code to login</p>
|
||||||
|
<h2>{{code}}</h2>
|
||||||
|
<p>Please not that this code expires after 10 minutes</p>
|
@ -2,6 +2,9 @@
|
|||||||
|
|
||||||
const hashPassword = require("../../functions/hashPassword");
|
const hashPassword = require("../../functions/hashPassword");
|
||||||
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
||||||
|
const nodemailer = require("nodemailer");
|
||||||
|
const fs = require("fs");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -9,9 +12,23 @@ const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
|||||||
* @param {string} param0.email
|
* @param {string} param0.email
|
||||||
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} [param0.dbSchema]
|
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} [param0.dbSchema]
|
||||||
* @param {string} param0.email_login_field
|
* @param {string} param0.email_login_field
|
||||||
|
* @param {string} param0.mail_domain
|
||||||
|
* @param {string} param0.mail_username
|
||||||
|
* @param {string} param0.mail_password
|
||||||
|
* @param {number} [param0.mail_port]
|
||||||
|
* @param {string} [param0.sender]
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
async function localSendEmailCode({ email, dbSchema, email_login_field }) {
|
async function localSendEmailCode({
|
||||||
|
email,
|
||||||
|
dbSchema,
|
||||||
|
email_login_field,
|
||||||
|
mail_domain,
|
||||||
|
mail_username,
|
||||||
|
mail_password,
|
||||||
|
mail_port,
|
||||||
|
sender,
|
||||||
|
}) {
|
||||||
try {
|
try {
|
||||||
/**
|
/**
|
||||||
* User auth
|
* User auth
|
||||||
@ -71,9 +88,43 @@ async function localSendEmailCode({ email, dbSchema, email_login_field }) {
|
|||||||
|
|
||||||
if (foundUser && foundUser[0] && email_login_field) {
|
if (foundUser && foundUser[0] && email_login_field) {
|
||||||
const tempCode = generateCode();
|
const tempCode = generateCode();
|
||||||
|
|
||||||
|
let transporter = nodemailer.createTransport({
|
||||||
|
host: mail_domain,
|
||||||
|
port: mail_port || 465,
|
||||||
|
secure: true,
|
||||||
|
auth: {
|
||||||
|
user: mail_username,
|
||||||
|
pass: mail_password,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
let mailObject = {};
|
||||||
|
|
||||||
|
mailObject["from"] = `"Datasquirel SSO" <${
|
||||||
|
sender || "support@datasquirel.com"
|
||||||
|
}>`;
|
||||||
|
mailObject["sender"] = sender || "support@summitlending.com";
|
||||||
|
mailObject["to"] = email;
|
||||||
|
mailObject["subject"] = "One Time Email Login Code";
|
||||||
|
mailObject["html"] = fs
|
||||||
|
.readFileSync(
|
||||||
|
path.resolve(__dirname, "one-time-code.html"),
|
||||||
|
"utf-8"
|
||||||
|
)
|
||||||
|
.replace(/{{code}}/, tempCode);
|
||||||
|
|
||||||
|
const info = await transporter.sendMail(mailObject);
|
||||||
|
|
||||||
|
if (!info?.accepted) throw new Error("Mail not Sent!");
|
||||||
|
|
||||||
|
/** ********************************************** */
|
||||||
|
/** ********************************************** */
|
||||||
|
/** ********************************************** */
|
||||||
|
|
||||||
let setTempCode = await varDatabaseDbHandler({
|
let setTempCode = await varDatabaseDbHandler({
|
||||||
queryString: `UPDATE users SET ${email_login_field} = ? WHERE email = ?`,
|
queryString: `UPDATE users SET ${email_login_field} = ? WHERE email = ?`,
|
||||||
queryValuesArray: [tempCode, email],
|
queryValuesArray: [tempCode + `-${Date.now()}`, email],
|
||||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||||
tableSchema,
|
tableSchema,
|
||||||
});
|
});
|
||||||
|
@ -41,6 +41,11 @@ const localSendEmailCode = require("../engine/user/send-email-code");
|
|||||||
* @param {String} params.encryptionKey - Encryption Key
|
* @param {String} params.encryptionKey - Encryption Key
|
||||||
* @param {String} params.encryptionSalt - Encryption Salt
|
* @param {String} params.encryptionSalt - Encryption Salt
|
||||||
* @param {string} [params.temp_code_field] - Database table field name for temporary code
|
* @param {string} [params.temp_code_field] - Database table field name for temporary code
|
||||||
|
* @param {string} params.mail_domain
|
||||||
|
* @param {string} params.mail_username
|
||||||
|
* @param {string} params.mail_password
|
||||||
|
* @param {number} [params.mail_port]
|
||||||
|
* @param {string} [params.sender]
|
||||||
*
|
*
|
||||||
* @returns { Promise<boolean>}
|
* @returns { Promise<boolean>}
|
||||||
*/
|
*/
|
||||||
@ -51,6 +56,11 @@ async function sendEmailCode({
|
|||||||
encryptionKey,
|
encryptionKey,
|
||||||
encryptionSalt,
|
encryptionSalt,
|
||||||
temp_code_field,
|
temp_code_field,
|
||||||
|
mail_domain,
|
||||||
|
mail_password,
|
||||||
|
mail_username,
|
||||||
|
mail_port,
|
||||||
|
sender,
|
||||||
}) {
|
}) {
|
||||||
const scheme = process.env.DSQL_HTTP_SCHEME;
|
const scheme = process.env.DSQL_HTTP_SCHEME;
|
||||||
const localHost = process.env.DSQL_LOCAL_HOST;
|
const localHost = process.env.DSQL_LOCAL_HOST;
|
||||||
@ -116,6 +126,11 @@ async function sendEmailCode({
|
|||||||
email,
|
email,
|
||||||
dbSchema,
|
dbSchema,
|
||||||
email_login_field: emailLoginTempCodeFieldName,
|
email_login_field: emailLoginTempCodeFieldName,
|
||||||
|
mail_domain,
|
||||||
|
mail_password,
|
||||||
|
mail_username,
|
||||||
|
mail_port,
|
||||||
|
sender,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -131,6 +146,18 @@ async function sendEmailCode({
|
|||||||
email,
|
email,
|
||||||
database,
|
database,
|
||||||
email_login_field: emailLoginTempCodeFieldName,
|
email_login_field: emailLoginTempCodeFieldName,
|
||||||
|
mail_domain,
|
||||||
|
mail_password,
|
||||||
|
mail_username,
|
||||||
|
mail_port,
|
||||||
|
sender,
|
||||||
|
html: fs.readFileSync(
|
||||||
|
path.resolve(
|
||||||
|
__dirname,
|
||||||
|
"../../engine/user/one-time-code.html"
|
||||||
|
),
|
||||||
|
"utf-8"
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
const httpsRequest = (
|
const httpsRequest = (
|
||||||
|
Loading…
Reference in New Issue
Block a user