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
engine/user
users

View File

@ -22,18 +22,25 @@ const runQuery = require("../query/utils/runQuery");
* @param {Object} params - Single object passed
* @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 {string} [params.encryptionKey]
* @param {string} [params.encryptionSalt]
*
* @returns { Promise<LocalPostReturn> } - Return Object
*/
async function localAddUser({ payload, dbSchema }) {
async function localAddUser({
payload,
dbSchema,
encryptionKey,
encryptionSalt,
}) {
try {
/**
* Initialize Variables
*/
const dbFullName = process.env.DSQL_DB_NAME || "";
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
const encryptionKeyFinal = process.env.DSQL_ENCRYPTION_KEY || "";
const encryptionSaltFinal = process.env.DSQL_ENCRYPTION_SALT || "";
/**
* Hash Password
@ -41,12 +48,15 @@ async function localAddUser({ payload, dbSchema }) {
* @description Hash 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({
password: payload.password,
encryptionKey,
encryptionKey: encryptionKeyFinal,
});
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;
@ -86,14 +98,23 @@ async function localAddUser({ payload, dbSchema }) {
}
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({
queryString: `SELECT * FROM users WHERE email = ?${payload.username ? "OR username = ?" : ""}}`,
queryValuesArray: payload.username ? [payload.email, payload.username] : [payload.email],
queryString: `SELECT * FROM users WHERE email = ?${
payload.username ? "OR username = ?" : ""
}}`,
queryValuesArray: payload.username
? [payload.email, payload.username]
: [payload.email],
database: dbFullName,
tableSchema: tableSchema,
});
@ -111,10 +132,11 @@ async function localAddUser({ payload, dbSchema }) {
data: {
...payload,
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,
encryptionSalt,
encryptionKey: encryptionKeyFinal,
encryptionSalt: encryptionSaltFinal,
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.database - Database Name
* @param {UserDataPayload} props.payload - User Data Payload
* @param {string} props.encryptionKey
* @param {string} [props.encryptionSalt]
*
* @returns { Promise<FunctionReturn> }
*/
async function addUser({ key, payload, database }) {
async function addUser({
key,
payload,
database,
encryptionKey,
encryptionSalt,
}) {
/**
* Check for local DB settings
*
@ -96,6 +104,8 @@ async function addUser({ key, payload, database }) {
return await localAddUser({
dbSchema: dbSchema,
payload: payload,
encryptionKey,
encryptionSalt,
});
}
}
@ -109,6 +119,7 @@ async function addUser({ key, payload, database }) {
const reqPayload = JSON.stringify({
payload,
database,
encryptionKey,
});
const httpsRequest = (scheme?.match(/^http$/i) ? http : https).request(