This commit is contained in:
Benjamin Toby 2024-08-22 12:59:50 +01:00
parent 9fe7c765c6
commit c41e94e7eb
2 changed files with 47 additions and 14 deletions

View File

@ -22,18 +22,25 @@ const runQuery = require("../query/utils/runQuery");
* @param {Object} params - Single object passed * @param {Object} params - Single object passed
* @param {import("../../users/add-user").UserDataPayload} params.payload - SQL Query * @param {import("../../users/add-user").UserDataPayload} params.payload - SQL Query
* @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} params.dbSchema - Name of the table to query * @param {import("../../types/database-schema.td").DSQL_DatabaseSchemaType} params.dbSchema - Name of the table to query
* @param {string} [params.encryptionKey]
* @param {string} [params.encryptionSalt]
* *
* @returns { Promise<LocalPostReturn> } - Return Object * @returns { Promise<LocalPostReturn> } - Return Object
*/ */
async function localAddUser({ payload, dbSchema }) { async function localAddUser({
payload,
dbSchema,
encryptionKey,
encryptionSalt,
}) {
try { try {
/** /**
* Initialize Variables * Initialize Variables
*/ */
const dbFullName = process.env.DSQL_DB_NAME || ""; const dbFullName = process.env.DSQL_DB_NAME || "";
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || ""; const encryptionKeyFinal = process.env.DSQL_ENCRYPTION_KEY || "";
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || ""; const encryptionSaltFinal = process.env.DSQL_ENCRYPTION_SALT || "";
/** /**
* Hash Password * Hash Password
@ -41,12 +48,15 @@ async function localAddUser({ payload, dbSchema }) {
* @description Hash Password * @description Hash Password
*/ */
if (!payload?.password) { if (!payload?.password) {
return { success: false, payload: `Password is required to create an account` }; return {
success: false,
payload: `Password is required to create an account`,
};
} }
const hashedPassword = hashPassword({ const hashedPassword = hashPassword({
password: payload.password, password: payload.password,
encryptionKey, encryptionKey: encryptionKeyFinal,
}); });
payload.password = hashedPassword; payload.password = hashedPassword;
@ -73,7 +83,9 @@ async function localAddUser({ payload, dbSchema }) {
}; };
} }
const fieldsTitles = fields.map((/** @type {*} */ fieldObject) => fieldObject.Field); const fieldsTitles = fields.map(
(/** @type {*} */ fieldObject) => fieldObject.Field
);
let invalidField = null; let invalidField = null;
@ -86,14 +98,23 @@ async function localAddUser({ payload, dbSchema }) {
} }
if (invalidField) { if (invalidField) {
return { success: false, payload: `${invalidField} is not a valid field!` }; return {
success: false,
payload: `${invalidField} is not a valid field!`,
};
} }
const tableSchema = dbSchema.tables.find((tb) => tb?.tableName === "users"); const tableSchema = dbSchema.tables.find(
(tb) => tb?.tableName === "users"
);
const existingUser = await varDatabaseDbHandler({ const existingUser = await varDatabaseDbHandler({
queryString: `SELECT * FROM users WHERE email = ?${payload.username ? "OR username = ?" : ""}}`, queryString: `SELECT * FROM users WHERE email = ?${
queryValuesArray: payload.username ? [payload.email, payload.username] : [payload.email], payload.username ? "OR username = ?" : ""
}}`,
queryValuesArray: payload.username
? [payload.email, payload.username]
: [payload.email],
database: dbFullName, database: dbFullName,
tableSchema: tableSchema, tableSchema: tableSchema,
}); });
@ -111,10 +132,11 @@ async function localAddUser({ payload, dbSchema }) {
data: { data: {
...payload, ...payload,
image: "/images/user_images/user-preset.png", image: "/images/user_images/user-preset.png",
image_thumbnail: "/images/user_images/user-preset-thumbnail.png", image_thumbnail:
"/images/user_images/user-preset-thumbnail.png",
}, },
encryptionKey, encryptionKey: encryptionKeyFinal,
encryptionSalt, encryptionSalt: encryptionSaltFinal,
tableSchema, tableSchema,
}); });

View File

@ -52,10 +52,18 @@ const localAddUser = require("../engine/user/add-user");
* @param {string} props.key - FULL ACCESS API Key * @param {string} props.key - FULL ACCESS API Key
* @param {string} props.database - Database Name * @param {string} props.database - Database Name
* @param {UserDataPayload} props.payload - User Data Payload * @param {UserDataPayload} props.payload - User Data Payload
* @param {string} props.encryptionKey
* @param {string} [props.encryptionSalt]
* *
* @returns { Promise<FunctionReturn> } * @returns { Promise<FunctionReturn> }
*/ */
async function addUser({ key, payload, database }) { async function addUser({
key,
payload,
database,
encryptionKey,
encryptionSalt,
}) {
/** /**
* Check for local DB settings * Check for local DB settings
* *
@ -96,6 +104,8 @@ async function addUser({ key, payload, database }) {
return await localAddUser({ return await localAddUser({
dbSchema: dbSchema, dbSchema: dbSchema,
payload: payload, payload: payload,
encryptionKey,
encryptionSalt,
}); });
} }
} }
@ -109,6 +119,7 @@ async function addUser({ key, payload, database }) {
const reqPayload = JSON.stringify({ const reqPayload = JSON.stringify({
payload, payload,
database, database,
encryptionKey,
}); });
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request( const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(