Updates
This commit is contained in:
parent
6df20790f4
commit
8ca2779741
@ -10,8 +10,8 @@ require("dotenv").config({
|
||||
});
|
||||
|
||||
const datasquirel = require("../index");
|
||||
const createDbFromSchema = require("./engine/createDbFromSchema");
|
||||
const colors = require("../console-colors");
|
||||
const createDbFromSchema = require("../package-shared/shell/createDbFromSchema");
|
||||
|
||||
if (!fs.existsSync(path.resolve(process.cwd(), ".env"))) {
|
||||
console.log(".env file not found");
|
||||
@ -26,8 +26,6 @@ const {
|
||||
DSQL_KEY,
|
||||
DSQL_REF_DB_NAME,
|
||||
DSQL_FULL_SYNC,
|
||||
DSQL_ENCRYPTION_KEY,
|
||||
DSQL_ENCRYPTION_SALT,
|
||||
} = process.env;
|
||||
|
||||
if (!DSQL_HOST?.match(/./)) {
|
||||
@ -123,7 +121,10 @@ async function run() {
|
||||
);
|
||||
|
||||
// deepcode ignore reDOS: <please specify a reason of ignoring this>
|
||||
await createDbFromSchema(schemaData);
|
||||
await createDbFromSchema({
|
||||
dbSchemaData: schemaData,
|
||||
});
|
||||
|
||||
console.log(
|
||||
` - ${colors.FgGreen}Success:${colors.Reset} Databases created Successfully!`
|
||||
);
|
||||
|
@ -18,17 +18,7 @@ const mysqlDumpPath = process.platform?.match(/win/i)
|
||||
"'"
|
||||
: "mysqldump";
|
||||
|
||||
const {
|
||||
DSQL_HOST,
|
||||
DSQL_USER,
|
||||
DSQL_PASS,
|
||||
DSQL_DB_NAME,
|
||||
DSQL_KEY,
|
||||
DSQL_REF_DB_NAME,
|
||||
DSQL_FULL_SYNC,
|
||||
DSQL_ENCRYPTION_KEY,
|
||||
DSQL_ENCRYPTION_SALT,
|
||||
} = process.env;
|
||||
const { DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
|
||||
|
||||
const dbName = DSQL_DB_NAME || "";
|
||||
const dumpFilePathArg = process.argv.indexOf("--file");
|
||||
|
4
engine/engine/addUsersTableToDb.d.ts
vendored
4
engine/engine/addUsersTableToDb.d.ts
vendored
@ -1,4 +0,0 @@
|
||||
declare function _exports({ dbSchema }: {
|
||||
dbSchema: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
}): Promise<any>;
|
||||
export = _exports;
|
@ -1,88 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { execSync } = require("child_process");
|
||||
const updateApiSchemaFromLocalDb = require("../query/update-api-schema-from-local-db");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Add `users` table to user database
|
||||
* ==============================================================================
|
||||
*
|
||||
* @param {object} params - Single object passed
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} params.dbSchema - Database Schema Object
|
||||
*
|
||||
* @returns {Promise<*>} new user auth object payload
|
||||
*/
|
||||
module.exports = async function addUsersTableToDb({ dbSchema }) {
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* @description Initialize
|
||||
*/
|
||||
const database = process.env.DSQL_DB_NAME || "";
|
||||
/** @type {import("../../package-shared/types").DSQL_TableSchemaType} */
|
||||
const userPreset = require("./data/presets/users.json");
|
||||
|
||||
try {
|
||||
/**
|
||||
* Fetch user
|
||||
*
|
||||
* @description Fetch user from db
|
||||
*/
|
||||
const userSchemaMainFilePath = path.resolve(
|
||||
process.cwd(),
|
||||
"dsql.schema.json"
|
||||
);
|
||||
let targetDatabase = dbSchema;
|
||||
|
||||
if (!targetDatabase) throw new Error("Target database not found!");
|
||||
|
||||
let existingTableIndex = targetDatabase.tables.findIndex(
|
||||
(table, index) => {
|
||||
if (table.tableName === "users") {
|
||||
existingTableIndex = index;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
if (existingTableIndex >= 0) {
|
||||
targetDatabase.tables[existingTableIndex] = userPreset;
|
||||
} else {
|
||||
targetDatabase.tables.push(userPreset);
|
||||
}
|
||||
|
||||
fs.writeFileSync(
|
||||
`${userSchemaMainFilePath}`,
|
||||
JSON.stringify(dbSchema, null, 4),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
await updateApiSchemaFromLocalDb();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
@ -1,300 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const path = require("path");
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
const varDatabaseDbHandler = require("./utils/varDatabaseDbHandler");
|
||||
const createTable = require("./utils/createTable");
|
||||
const updateTable = require("./utils/updateTable");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Create database from Schema
|
||||
* ==============================================================================
|
||||
* @description Create database from Schema. This function is called when the user
|
||||
* runs the "dsql create" command. `NOTE`: there must be a "dsql.schema.json" file
|
||||
* in the root of the project for this function to work
|
||||
*
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} dbSchema - An array of database schema objects
|
||||
*/
|
||||
async function createDbFromSchema(dbSchema) {
|
||||
try {
|
||||
/**
|
||||
* Grab Schema
|
||||
*
|
||||
* @description Grab Schema
|
||||
*/
|
||||
|
||||
if (!dbSchema || !Array.isArray(dbSchema) || !dbSchema[0]) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
/** @type {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
|
||||
const database = dbSchema[i];
|
||||
|
||||
if (!database) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { dbFullName, tables } = database;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** @type {{ dbFullName: string }[] | null} */
|
||||
const dbCheck = await noDatabaseDbHandler({
|
||||
query: `SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`,
|
||||
});
|
||||
|
||||
if (dbCheck && dbCheck[0]?.dbFullName) {
|
||||
// Database Exists
|
||||
} else {
|
||||
const newDatabase = await noDatabaseDbHandler({
|
||||
query: `CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`,
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Select all tables
|
||||
* @type {{ TABLE_NAME: string }[] | null}
|
||||
* @description Select All tables in target database
|
||||
*/
|
||||
const allTables = await noDatabaseDbHandler({
|
||||
query: `SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`,
|
||||
});
|
||||
|
||||
let tableDropped;
|
||||
|
||||
if (!allTables) {
|
||||
console.log("No Tables to Update");
|
||||
continue;
|
||||
}
|
||||
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
|
||||
/**
|
||||
* @description Check if TABLE_NAME is part of the tables contained
|
||||
* in the user schema JSON. If it's not, the table is either deleted
|
||||
* or the table name has been recently changed
|
||||
*/
|
||||
if (
|
||||
!tables.filter(
|
||||
(_table) => _table.tableName === TABLE_NAME
|
||||
)[0]
|
||||
) {
|
||||
const oldTableFilteredArray = tables.filter(
|
||||
(_table) =>
|
||||
_table.tableNameOld &&
|
||||
_table.tableNameOld === TABLE_NAME
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Check if this table has been recently renamed. Rename
|
||||
* table id true. Drop table if false
|
||||
*/
|
||||
if (oldTableFilteredArray && oldTableFilteredArray[0]) {
|
||||
console.log("Renaming Table");
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `RENAME TABLE \`${oldTableFilteredArray[0].tableNameOld}\` TO \`${oldTableFilteredArray[0].tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
} else {
|
||||
console.log(`Dropping Table from ${dbFullName}`);
|
||||
// deepcode ignore reDOS: <NO user input>
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `DROP TABLE \`${TABLE_NAME}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
tableDropped = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
if (tableDropped) continue;
|
||||
|
||||
const { tableName, fields, indexes } = table;
|
||||
|
||||
/**
|
||||
* @description Check if table exists
|
||||
*/
|
||||
const tableCheck = await varDatabaseDbHandler({
|
||||
queryString: `
|
||||
SELECT EXISTS (
|
||||
SELECT
|
||||
TABLE_NAME
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = ? AND
|
||||
TABLE_NAME = ?
|
||||
) AS tableExists`,
|
||||
queryValuesArray: [dbFullName, table.tableName],
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (tableCheck && tableCheck[0]?.tableExists > 0) {
|
||||
/**
|
||||
* @description Update table if table exists
|
||||
*/
|
||||
const updateExistingTable = await updateTable({
|
||||
dbFullName: dbFullName,
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
tableIndex: t,
|
||||
});
|
||||
|
||||
if (table.childrenTables && table.childrenTables[0]) {
|
||||
for (
|
||||
let ch = 0;
|
||||
ch < table.childrenTables.length;
|
||||
ch++
|
||||
) {
|
||||
const childTable = table.childrenTables[ch];
|
||||
|
||||
const updateExistingChildTable = await updateTable({
|
||||
dbFullName: childTable.dbNameFull,
|
||||
tableName: childTable.tableName,
|
||||
tableInfoArray: fields,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
clone: true,
|
||||
});
|
||||
|
||||
// console.log(updateExistingChildTable);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
} else {
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
const createNewTable = await createTable({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
varDatabaseDbHandler,
|
||||
dbFullName: dbFullName,
|
||||
dbSchema,
|
||||
});
|
||||
|
||||
if (indexes && indexes[0]) {
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes && indexes[0]) {
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const {
|
||||
indexType,
|
||||
indexName,
|
||||
indexTableFields,
|
||||
alias,
|
||||
} = indexes[g];
|
||||
|
||||
if (!alias?.match(/./)) continue;
|
||||
|
||||
/**
|
||||
* @type {any[] | null}
|
||||
* @description All indexes from MYSQL db
|
||||
*/
|
||||
const allExistingIndexes =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
const existingKeyInDb = allExistingIndexes
|
||||
? allExistingIndexes.filter(
|
||||
(indexObject) =>
|
||||
indexObject.Key_name === alias
|
||||
)
|
||||
: null;
|
||||
if (!existingKeyInDb?.[0])
|
||||
throw new Error(
|
||||
"This Index Does not Exist"
|
||||
);
|
||||
} catch (error) {
|
||||
/**
|
||||
* @description Create new index if determined that it
|
||||
* doesn't exist in MYSQL db
|
||||
*/
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `CREATE${
|
||||
indexType?.match(/fullText/i)
|
||||
? " FULLTEXT"
|
||||
: ""
|
||||
} INDEX \`${alias}\` ON ${tableName}(${indexTableFields
|
||||
?.map((nm) => nm.value)
|
||||
.map((nm) => `\`${nm}\``)
|
||||
.join(
|
||||
","
|
||||
)}) COMMENT 'schema_index'`,
|
||||
database: dbFullName,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Error in createDbFromSchema => ", error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
module.exports = createDbFromSchema;
|
12
engine/engine/utils/camelJoinedtoCamelSpace.d.ts
vendored
12
engine/engine/utils/camelJoinedtoCamelSpace.d.ts
vendored
@ -1,12 +0,0 @@
|
||||
export = camelJoinedtoCamelSpace;
|
||||
/**
|
||||
* Convert Camel Joined Text to Camel Spaced Text
|
||||
* ==============================================================================
|
||||
* @description this function takes a camel cased text without spaces, and returns
|
||||
* a camel-case-spaced text
|
||||
*
|
||||
* @param {string} text - text string without spaces
|
||||
*
|
||||
* @returns {string | null}
|
||||
*/
|
||||
declare function camelJoinedtoCamelSpace(text: string): string | null;
|
8
engine/engine/utils/dbHandler.d.ts
vendored
8
engine/engine/utils/dbHandler.d.ts
vendored
@ -1,8 +0,0 @@
|
||||
declare function _exports({ query, values, database, dbSchema, tableName, }: {
|
||||
query: string;
|
||||
values?: (string | number)[];
|
||||
dbSchema?: import("../../../package-shared/types").DSQL_DatabaseSchemaType;
|
||||
database?: string;
|
||||
tableName?: string;
|
||||
}): Promise<any>;
|
||||
export = _exports;
|
@ -1,173 +0,0 @@
|
||||
/** # MODULE TRACE
|
||||
======================================================================
|
||||
* Detected 8 files that call this module. The files are listed below:
|
||||
======================================================================
|
||||
* `require` Statement Found in [noDatabaseDbHandler.js](d:\GitHub\dsql\engine\engine\utils\noDatabaseDbHandler.js)
|
||||
* `require` Statement Found in [varDatabaseDbHandler.js](d:\GitHub\dsql\engine\engine\utils\varDatabaseDbHandler.js)
|
||||
* `require` Statement Found in [addDbEntry.js](d:\GitHub\dsql\engine\query\utils\addDbEntry.js)
|
||||
* `require` Statement Found in [deleteDbEntry.js](d:\GitHub\dsql\engine\query\utils\deleteDbEntry.js)
|
||||
* `require` Statement Found in [runQuery.js](d:\GitHub\dsql\engine\query\utils\runQuery.js)
|
||||
* `require` Statement Found in [updateDbEntry.js](d:\GitHub\dsql\engine\query\utils\updateDbEntry.js)
|
||||
* `require` Statement Found in [githubLogin.js](d:\GitHub\dsql\engine\user\social\utils\githubLogin.js)
|
||||
* `require` Statement Found in [googleLogin.js](d:\GitHub\dsql\engine\user\social\utils\googleLogin.js)
|
||||
==== MODULE TRACE END ==== */
|
||||
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const fs = require("fs");
|
||||
const mysql = require("mysql");
|
||||
const path = require("path");
|
||||
const grabDbSSL = require("../../../package-shared/utils/backend/grabDbSSL");
|
||||
|
||||
const connection = mysql.createConnection({
|
||||
host: process.env.DSQL_HOST,
|
||||
user: process.env.DSQL_USER,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
password: process.env.DSQL_PASS,
|
||||
charset: "utf8mb4",
|
||||
port: process.env.DSQL_PORT?.match(/.../)
|
||||
? parseInt(process.env.DSQL_PORT)
|
||||
: undefined,
|
||||
timeout: 5000,
|
||||
ssl: grabDbSSL(),
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Main DB Handler Function
|
||||
* ==============================================================================
|
||||
* @async
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {string} params.query - Query String
|
||||
* @param {(string | number)[]} [params.values] - Values
|
||||
* @param {import("../../../package-shared/types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database Schema
|
||||
* @param {string} [params.database] - Target Database
|
||||
* @param {string} [params.tableName] - Target Table Name
|
||||
*
|
||||
* @returns {Promise<*>}
|
||||
*/
|
||||
module.exports = async function dbHandler({
|
||||
query,
|
||||
values,
|
||||
database,
|
||||
dbSchema,
|
||||
tableName,
|
||||
}) {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
let changeDbError;
|
||||
|
||||
if (database) {
|
||||
connection.changeUser({ database: database }, (error) => {
|
||||
if (error) {
|
||||
console.log(
|
||||
"DB handler error in switching database:",
|
||||
error.message
|
||||
);
|
||||
changeDbError = error.message;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (changeDbError) {
|
||||
return { error: changeDbError };
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
results = await new Promise((resolve, reject) => {
|
||||
if (values?.[0]) {
|
||||
connection.query(query, values, (error, results, fields) => {
|
||||
if (error) {
|
||||
console.log(
|
||||
"DB handler error with values array:",
|
||||
error.message
|
||||
);
|
||||
console.log("SQL:", error.sql);
|
||||
console.log("State:", error.sqlState, error.sqlMessage);
|
||||
|
||||
resolve({
|
||||
error: error.message,
|
||||
});
|
||||
} else {
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
}
|
||||
|
||||
// setTimeout(() => {
|
||||
// endConnection(connection);
|
||||
// }, 500);
|
||||
});
|
||||
} else {
|
||||
connection.query(query, (error, results, fields) => {
|
||||
if (error) {
|
||||
console.log("DB handler error:", error.message);
|
||||
console.log("SQL:", error.sql);
|
||||
console.log("State:", error.sqlState, error.sqlMessage);
|
||||
|
||||
resolve({
|
||||
error: error.message,
|
||||
});
|
||||
} else {
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
}
|
||||
// setTimeout(() => {
|
||||
// endConnection(connection);
|
||||
// }, 500);
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("DB handler error:", error.message);
|
||||
|
||||
results = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
// if (results && dbSchema && tableName) {
|
||||
// const tableSchema = dbSchema.tables.find((table) => table.tableName === tableName);
|
||||
// const parsedResults = parseDbResults({
|
||||
// unparsedResults: results,
|
||||
// tableSchema: tableSchema,
|
||||
// });
|
||||
|
||||
// return parsedResults;
|
||||
// } else
|
||||
if (results) {
|
||||
return results;
|
||||
} else {
|
||||
console.log("DSQL DB handler no results received for Query =>", query);
|
||||
return null;
|
||||
}
|
||||
};
|
8
engine/engine/utils/defaultFieldsRegexp.d.ts
vendored
8
engine/engine/utils/defaultFieldsRegexp.d.ts
vendored
@ -1,8 +0,0 @@
|
||||
export = defaultFieldsRegexp;
|
||||
/**
|
||||
* Regular expression to match default fields
|
||||
*
|
||||
* @description Regular expression to match default fields
|
||||
* @type {RegExp}
|
||||
*/
|
||||
declare const defaultFieldsRegexp: RegExp;
|
@ -1,13 +0,0 @@
|
||||
/**
|
||||
* Regular expression to match default fields
|
||||
*
|
||||
* @description Regular expression to match default fields
|
||||
* @type {RegExp}
|
||||
*/
|
||||
const defaultFieldsRegexp = /^id$|^date_created$|^date_created_code$|^date_created_timestamp$|^date_updated$|^date_updated_code$|^date_updated_timestamp$/;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
module.exports = defaultFieldsRegexp;
|
@ -1,92 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const mysql = require("mysql");
|
||||
const path = require("path");
|
||||
const grabDbSSL = require("../../../package-shared/utils/backend/grabDbSSL");
|
||||
|
||||
const connection = mysql.createConnection({
|
||||
host: process.env.DSQL_HOST,
|
||||
user: process.env.DSQL_USER,
|
||||
password: process.env.DSQL_PASS,
|
||||
charset: "utf8mb4",
|
||||
port: process.env.DSQL_PORT?.match(/.../)
|
||||
? parseInt(process.env.DSQL_PORT)
|
||||
: undefined,
|
||||
timeout: 5000,
|
||||
ssl: grabDbSSL(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single Param object containing params
|
||||
* @param {string} params.query - Query String
|
||||
* @param {string[]} [params.values] - Values
|
||||
*
|
||||
* @returns {Promise<any[] | null>}
|
||||
*/
|
||||
module.exports = async function noDatabaseDbHandler({ query, values }) {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
/** ********************* Run Query */
|
||||
results = await new Promise((resolve, reject) => {
|
||||
if (values) {
|
||||
connection.query(query, values, (error, results, fields) => {
|
||||
if (error) {
|
||||
console.log("NO-DB handler error:", error.message);
|
||||
resolve({
|
||||
error: error.message,
|
||||
});
|
||||
} else {
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
}
|
||||
// setTimeout(() => {
|
||||
// endConnection(connection);
|
||||
// }, 500);
|
||||
});
|
||||
} else {
|
||||
connection.query(query, (error, results, fields) => {
|
||||
if (error) {
|
||||
console.log("NO-DB handler error:", error.message);
|
||||
resolve({
|
||||
error: error.message,
|
||||
});
|
||||
} else {
|
||||
resolve(JSON.parse(JSON.stringify(results)));
|
||||
}
|
||||
// setTimeout(() => {
|
||||
// endConnection(connection);
|
||||
// }, 500);
|
||||
});
|
||||
}
|
||||
});
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("ERROR in noDatabaseDbHandler =>", error.message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results) {
|
||||
return results;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
5
engine/engine/utils/parseDbResults.d.ts
vendored
5
engine/engine/utils/parseDbResults.d.ts
vendored
@ -1,5 +0,0 @@
|
||||
declare function _exports({ unparsedResults, tableSchema, }: {
|
||||
unparsedResults: any[];
|
||||
tableSchema?: import("../../../package-shared/types").DSQL_TableSchemaType;
|
||||
}): Promise<object[] | null>;
|
||||
export = _exports;
|
@ -1,83 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const decrypt = require("../../../functions/decrypt");
|
||||
// @ts-ignore
|
||||
const defaultFieldsRegexp = require("./defaultFieldsRegexp");
|
||||
|
||||
/**
|
||||
* Parse Database results
|
||||
* ==============================================================================
|
||||
* @description this function takes a database results array gotten from a DB handler
|
||||
* function, decrypts encrypted fields, and returns an updated array with no encrypted
|
||||
* fields
|
||||
*
|
||||
* @param {object} params - Single object params
|
||||
* @param {*[]} params.unparsedResults - Array of data objects containing Fields(keys)
|
||||
* and corresponding values of the fields(values)
|
||||
* @param {import("../../../package-shared/types").DSQL_TableSchemaType} [params.tableSchema] - Table schema
|
||||
* @returns {Promise<object[]|null>}
|
||||
*/
|
||||
module.exports = async function parseDbResults({
|
||||
unparsedResults,
|
||||
tableSchema,
|
||||
}) {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
* @type {*[]}
|
||||
*/
|
||||
let parsedResults = [];
|
||||
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
try {
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
for (let pr = 0; pr < unparsedResults.length; pr++) {
|
||||
let result = unparsedResults[pr];
|
||||
|
||||
let resultFieldNames = Object.keys(result);
|
||||
|
||||
for (let i = 0; i < resultFieldNames.length; i++) {
|
||||
const resultFieldName = resultFieldNames[i];
|
||||
let resultFieldSchema = tableSchema?.fields[i];
|
||||
|
||||
if (resultFieldName?.match(defaultFieldsRegexp)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let value = result[resultFieldName];
|
||||
|
||||
if (typeof value !== "number" && !value) {
|
||||
// parsedResults.push(result);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (resultFieldSchema?.encrypted && value?.match(/./)) {
|
||||
result[resultFieldName] = decrypt({
|
||||
encryptedString: value,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
parsedResults.push(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
*/
|
||||
return parsedResults;
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("ERROR in parseDbResults Function =>", error.message);
|
||||
return unparsedResults;
|
||||
}
|
||||
};
|
23
engine/engine/utils/varDatabaseDbHandler.d.ts
vendored
23
engine/engine/utils/varDatabaseDbHandler.d.ts
vendored
@ -1,23 +0,0 @@
|
||||
declare namespace _exports {
|
||||
export { VarDbHandlerParam };
|
||||
}
|
||||
declare function _exports({ queryString, queryValuesArray, database, tableSchema, }: VarDbHandlerParam): Promise<any>;
|
||||
export = _exports;
|
||||
type VarDbHandlerParam = {
|
||||
/**
|
||||
* - SQL string
|
||||
*/
|
||||
queryString: string;
|
||||
/**
|
||||
* - Values Array
|
||||
*/
|
||||
queryValuesArray?: string[];
|
||||
/**
|
||||
* - Database name
|
||||
*/
|
||||
database: string;
|
||||
/**
|
||||
* - Table schema
|
||||
*/
|
||||
tableSchema?: import("../../../package-shared/types").DSQL_TableSchemaType;
|
||||
};
|
@ -1,129 +0,0 @@
|
||||
/** # MODULE TRACE
|
||||
======================================================================
|
||||
* Detected 9 files that call this module. The files are listed below:
|
||||
======================================================================
|
||||
* `require` Statement Found in [createDbFromSchema.js](d:\GitHub\dsql\engine\engine\createDbFromSchema.js)
|
||||
* `require` Statement Found in [updateTable.js](d:\GitHub\dsql\engine\engine\utils\updateTable.js)
|
||||
* `require` Statement Found in [runQuery.js](d:\GitHub\dsql\engine\query\utils\runQuery.js)
|
||||
* `require` Statement Found in [add-user.js](d:\GitHub\dsql\engine\user\add-user.js)
|
||||
* `require` Statement Found in [get-user.js](d:\GitHub\dsql\engine\user\get-user.js)
|
||||
* `require` Statement Found in [login-user.js](d:\GitHub\dsql\engine\user\login-user.js)
|
||||
* `require` Statement Found in [reauth-user.js](d:\GitHub\dsql\engine\user\reauth-user.js)
|
||||
* `require` Statement Found in [handleSocialDb.js](d:\GitHub\dsql\engine\user\social\utils\handleSocialDb.js)
|
||||
* `require` Statement Found in [update-user.js](d:\GitHub\dsql\engine\user\update-user.js)
|
||||
==== MODULE TRACE END ==== */
|
||||
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const parseDbResults = require("./parseDbResults");
|
||||
const dbHandler = require("./dbHandler");
|
||||
|
||||
/**
|
||||
* @typedef {object} VarDbHandlerParam
|
||||
* @property {string} queryString - SQL string
|
||||
* @property {string[]} [queryValuesArray] - Values Array
|
||||
* @property {string} database - Database name
|
||||
* @property {import("../../../package-shared/types").DSQL_TableSchemaType} [tableSchema] - Table schema
|
||||
*/
|
||||
|
||||
/**
|
||||
* DB handler for specific database
|
||||
* ==============================================================================
|
||||
* @async
|
||||
* @param {VarDbHandlerParam} params
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
module.exports = async function varDatabaseDbHandler({
|
||||
queryString,
|
||||
queryValuesArray,
|
||||
database,
|
||||
tableSchema,
|
||||
}) {
|
||||
/**
|
||||
* Create Connection
|
||||
*
|
||||
* @description Create Connection
|
||||
*/
|
||||
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
/**
|
||||
* Declare variables
|
||||
*
|
||||
* @description Declare "results" variable
|
||||
* @type {*}
|
||||
*/
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Fetch from db
|
||||
*
|
||||
* @description Fetch data from db if no cache
|
||||
*/
|
||||
try {
|
||||
if (
|
||||
queryString &&
|
||||
Array.isArray(queryValuesArray) &&
|
||||
queryValuesArray[0]
|
||||
) {
|
||||
results = await dbHandler({
|
||||
query: queryString,
|
||||
values: queryValuesArray,
|
||||
database: database,
|
||||
});
|
||||
} else if (queryString && !Array.isArray(queryValuesArray)) {
|
||||
results = await dbHandler({
|
||||
query: queryString,
|
||||
database: database,
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",
|
||||
database,
|
||||
error
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return results
|
||||
*
|
||||
* @description Return results add to cache if "req" param is passed
|
||||
*/
|
||||
if (results && tableSchema) {
|
||||
try {
|
||||
const unparsedResults = results;
|
||||
// deepcode ignore reDOS: <please specify a reason of ignoring this>
|
||||
const parsedResults = await parseDbResults({
|
||||
unparsedResults: unparsedResults,
|
||||
tableSchema: tableSchema,
|
||||
});
|
||||
return parsedResults;
|
||||
} catch (error) {
|
||||
console.log(
|
||||
"\x1b[31mvarDatabaseDbHandler ERROR\x1b[0m =>",
|
||||
database,
|
||||
error
|
||||
);
|
||||
return null;
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} else if (results) {
|
||||
return results;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
};
|
64
engine/query/get.d.ts
vendored
64
engine/query/get.d.ts
vendored
@ -1,64 +0,0 @@
|
||||
export = localGet;
|
||||
/**
|
||||
* @typedef {Object} LocalGetReturn
|
||||
* @property {boolean} success - Did the function run successfully?
|
||||
* @property {*} [payload] - GET request results
|
||||
* @property {string} [msg] - Message
|
||||
* @property {string} [error] - Error Message
|
||||
*/
|
||||
/**
|
||||
* @typedef {Object} LocalQueryObject
|
||||
* @property {string} query - Table Name
|
||||
* @property {string} [tableName] - Table Name
|
||||
* @property {string[]} [queryValues] - GET request results
|
||||
*/
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {LocalQueryObject} params.options - SQL Query
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [params.dbSchema] - Name of the table to query
|
||||
*
|
||||
* @returns { Promise<LocalGetReturn> } - Return Object
|
||||
*/
|
||||
declare function localGet({ options, dbSchema }: {
|
||||
options: LocalQueryObject;
|
||||
dbSchema?: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
}): Promise<LocalGetReturn>;
|
||||
declare namespace localGet {
|
||||
export { LocalGetReturn, LocalQueryObject };
|
||||
}
|
||||
type LocalGetReturn = {
|
||||
/**
|
||||
* - Did the function run successfully?
|
||||
*/
|
||||
success: boolean;
|
||||
/**
|
||||
* - GET request results
|
||||
*/
|
||||
payload?: any;
|
||||
/**
|
||||
* - Message
|
||||
*/
|
||||
msg?: string;
|
||||
/**
|
||||
* - Error Message
|
||||
*/
|
||||
error?: string;
|
||||
};
|
||||
type LocalQueryObject = {
|
||||
/**
|
||||
* - Table Name
|
||||
*/
|
||||
query: string;
|
||||
/**
|
||||
* - Table Name
|
||||
*/
|
||||
tableName?: string;
|
||||
/**
|
||||
* - GET request results
|
||||
*/
|
||||
queryValues?: string[];
|
||||
};
|
@ -1,94 +0,0 @@
|
||||
/** # MODULE TRACE
|
||||
======================================================================
|
||||
* No imports found for this Module
|
||||
==== MODULE TRACE END ==== */
|
||||
|
||||
const runQuery = require("../../package-shared/functions/backend/db/runQuery");
|
||||
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* @typedef {Object} LocalGetReturn
|
||||
* @property {boolean} success - Did the function run successfully?
|
||||
* @property {*} [payload] - GET request results
|
||||
* @property {string} [msg] - Message
|
||||
* @property {string} [error] - Error Message
|
||||
*/
|
||||
|
||||
/**
|
||||
* @typedef {Object} LocalQueryObject
|
||||
* @property {string} query - Table Name
|
||||
* @property {string} [tableName] - Table Name
|
||||
* @property {string[]} [queryValues] - GET request results
|
||||
*/
|
||||
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {LocalQueryObject} params.options - SQL Query
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [params.dbSchema] - Name of the table to query
|
||||
*
|
||||
* @returns { Promise<LocalGetReturn> } - Return Object
|
||||
*/
|
||||
async function localGet({ options, dbSchema }) {
|
||||
try {
|
||||
const { query, queryValues } = options;
|
||||
|
||||
/** @type {string | undefined | any } */
|
||||
const tableName = options?.tableName ? options.tableName : undefined;
|
||||
const dbFullName = process.env.DSQL_DB_NAME || "";
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let results;
|
||||
|
||||
try {
|
||||
let { result, error } = await runQuery({
|
||||
dbFullName: dbFullName,
|
||||
query: query,
|
||||
queryValuesArray: queryValues,
|
||||
dbSchema,
|
||||
tableName,
|
||||
local: true,
|
||||
readOnly: true,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
if (!result)
|
||||
throw new Error("No Result received for query => " + query);
|
||||
if (result?.error) throw new Error(result.error);
|
||||
|
||||
results = result;
|
||||
return { success: true, payload: results };
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
////////////////////////////////////////
|
||||
|
||||
console.log("Error in local get Request =>", error.message);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
////////////////////////////////////////
|
||||
console.log("Error in local get Request =>", error.message);
|
||||
|
||||
return { success: false, msg: "Something went wrong!" };
|
||||
|
||||
////////////////////////////////////////
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = localGet;
|
16
engine/query/post.d.ts
vendored
16
engine/query/post.d.ts
vendored
@ -1,16 +0,0 @@
|
||||
export = localPost;
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {import("../../package-shared/types").LocalPostQueryObject} params.options
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [params.dbSchema]
|
||||
*
|
||||
* @returns { Promise<import("../../package-shared/types").LocalPostReturn> }
|
||||
*/
|
||||
declare function localPost({ options, dbSchema }: {
|
||||
options: import("../../package-shared/types").LocalPostQueryObject;
|
||||
dbSchema?: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
}): Promise<import("../../package-shared/types").LocalPostReturn>;
|
@ -1,62 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const runQuery = require("../../package-shared/functions/backend/db/runQuery");
|
||||
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {import("../../package-shared/types").LocalPostQueryObject} params.options
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [params.dbSchema]
|
||||
*
|
||||
* @returns { Promise<import("../../package-shared/types").LocalPostReturn> }
|
||||
*/
|
||||
async function localPost({ options, dbSchema }) {
|
||||
try {
|
||||
/**
|
||||
* Grab Body
|
||||
*/
|
||||
const { query, tableName, queryValues } = options;
|
||||
const dbFullName = process.env.DSQL_DB_NAME || "";
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
try {
|
||||
let { result, error } = await runQuery({
|
||||
dbFullName: dbFullName,
|
||||
query: query,
|
||||
dbSchema: dbSchema,
|
||||
queryValuesArray: queryValues,
|
||||
tableName,
|
||||
local: true,
|
||||
});
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: result,
|
||||
error: error,
|
||||
};
|
||||
} catch (/** @type {*} */ error) {
|
||||
return {
|
||||
success: false,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Error in local post Request =>", error.message);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
msg: "Something went wrong!",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = localPost;
|
@ -1,38 +0,0 @@
|
||||
export = updateApiSchemaFromLocalDb;
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/**
|
||||
* @typedef {Object} PostReturn
|
||||
* @property {boolean} success - Did the function run successfully?
|
||||
* @property {*} [payload] - The Y Coordinate
|
||||
* @property {string} [error] - The Y Coordinate
|
||||
*/
|
||||
/**
|
||||
* # Update API Schema From Local DB
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @returns { Promise<PostReturn> } - Return Object
|
||||
*/
|
||||
declare function updateApiSchemaFromLocalDb(): Promise<PostReturn>;
|
||||
declare namespace updateApiSchemaFromLocalDb {
|
||||
export { PostReturn };
|
||||
}
|
||||
type PostReturn = {
|
||||
/**
|
||||
* - Did the function run successfully?
|
||||
*/
|
||||
success: boolean;
|
||||
/**
|
||||
* - The Y Coordinate
|
||||
*/
|
||||
payload?: any;
|
||||
/**
|
||||
* - The Y Coordinate
|
||||
*/
|
||||
error?: string;
|
||||
};
|
@ -1,149 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Imports
|
||||
*/
|
||||
const https = require("https");
|
||||
const http = require("http");
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
const grabHostNames = require("../../package-shared/utils/grab-host-names");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* @typedef {Object} PostReturn
|
||||
* @property {boolean} success - Did the function run successfully?
|
||||
* @property {*} [payload] - The Y Coordinate
|
||||
* @property {string} [error] - The Y Coordinate
|
||||
*/
|
||||
|
||||
/**
|
||||
* # Update API Schema From Local DB
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @returns { Promise<PostReturn> } - Return Object
|
||||
*/
|
||||
async function updateApiSchemaFromLocalDb() {
|
||||
try {
|
||||
/**
|
||||
* Initialize
|
||||
*/
|
||||
const dbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
|
||||
const key = process.env.DSQL_KEY || "";
|
||||
|
||||
const dbSchema = JSON.parse(fs.readFileSync(dbSchemaPath, "utf8"));
|
||||
const { host, port, scheme, user_id } = grabHostNames();
|
||||
|
||||
/**
|
||||
* Make https request
|
||||
*
|
||||
* @description make a request to datasquirel.com
|
||||
*/
|
||||
const httpResponse = await new Promise((resolve, reject) => {
|
||||
const reqPayloadString = JSON.stringify({
|
||||
schema: dbSchema,
|
||||
}).replace(/\n|\r|\n\r/gm, "");
|
||||
|
||||
try {
|
||||
JSON.parse(reqPayloadString);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.log(reqPayloadString);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
error: "Query object is invalid. Please Check query data values",
|
||||
};
|
||||
}
|
||||
|
||||
const reqPayload = reqPayloadString;
|
||||
|
||||
const httpsRequest = scheme.request(
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Content-Length": Buffer.from(reqPayload).length,
|
||||
Authorization:
|
||||
key ||
|
||||
process.env.DSQL_FULL_ACCESS_API_KEY ||
|
||||
process.env.DSQL_API_KEY,
|
||||
},
|
||||
port,
|
||||
hostname: host,
|
||||
path: `/api/query/${user_id}/update-schema-from-single-database`,
|
||||
},
|
||||
|
||||
/**
|
||||
* Callback Function
|
||||
*
|
||||
* @description https request callback
|
||||
*/
|
||||
(response) => {
|
||||
var str = "";
|
||||
|
||||
response.on("data", function (chunk) {
|
||||
str += chunk;
|
||||
});
|
||||
|
||||
response.on("end", function () {
|
||||
try {
|
||||
resolve(JSON.parse(str));
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.log("Fetched Payload =>", str);
|
||||
|
||||
resolve({
|
||||
success: false,
|
||||
payload: null,
|
||||
error: error,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
response.on("error", (err) => {
|
||||
resolve({
|
||||
success: false,
|
||||
payload: null,
|
||||
error: err.message,
|
||||
});
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
httpsRequest.write(reqPayload);
|
||||
|
||||
httpsRequest.on("error", (error) => {
|
||||
console.log("HTTPS request ERROR =>", error);
|
||||
});
|
||||
|
||||
httpsRequest.end();
|
||||
});
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
return httpResponse;
|
||||
} catch (/** @type {*} */ error) {
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
/** ********************************************** */
|
||||
|
||||
module.exports = updateApiSchemaFromLocalDb;
|
20
engine/user/add-user.d.ts
vendored
20
engine/user/add-user.d.ts
vendored
@ -1,20 +0,0 @@
|
||||
export = localAddUser;
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {import("../../package-shared/types").UserDataPayload} params.payload - SQL Query
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} params.dbSchema - Name of the table to query
|
||||
* @param {string} [params.encryptionKey]
|
||||
* @param {string} [params.encryptionSalt]
|
||||
*
|
||||
* @returns { Promise<import("../../package-shared/types").AddUserFunctionReturn> } - Return Object
|
||||
*/
|
||||
declare function localAddUser({ payload, dbSchema, encryptionKey, encryptionSalt, }: {
|
||||
payload: import("../../package-shared/types").UserDataPayload;
|
||||
dbSchema: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
encryptionKey?: string;
|
||||
encryptionSalt?: string;
|
||||
}): Promise<import("../../package-shared/types").AddUserFunctionReturn>;
|
@ -1,169 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const hashPassword = require("../../functions/hashPassword");
|
||||
const addDbEntry = require("../../package-shared/functions/backend/db/addDbEntry");
|
||||
const addUsersTableToDb = require("../engine/addUsersTableToDb");
|
||||
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
||||
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {import("../../package-shared/types").UserDataPayload} params.payload - SQL Query
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} params.dbSchema - Name of the table to query
|
||||
* @param {string} [params.encryptionKey]
|
||||
* @param {string} [params.encryptionSalt]
|
||||
*
|
||||
* @returns { Promise<import("../../package-shared/types").AddUserFunctionReturn> } - Return Object
|
||||
*/
|
||||
async function localAddUser({
|
||||
payload,
|
||||
dbSchema,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
}) {
|
||||
try {
|
||||
/**
|
||||
* Initialize Variables
|
||||
*/
|
||||
const dbFullName = process.env.DSQL_DB_NAME || "";
|
||||
|
||||
const encryptionKeyFinal = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSaltFinal = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
/**
|
||||
* Hash Password
|
||||
*
|
||||
* @description Hash Password
|
||||
*/
|
||||
if (!payload?.password) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `Password is required to create an account`,
|
||||
};
|
||||
}
|
||||
|
||||
const hashedPassword = hashPassword({
|
||||
password: payload.password,
|
||||
encryptionKey: encryptionKeyFinal,
|
||||
});
|
||||
payload.password = hashedPassword;
|
||||
|
||||
let fields = await varDatabaseDbHandler({
|
||||
queryString: `SHOW COLUMNS FROM users`,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
if (!fields) {
|
||||
const newTable = await addUsersTableToDb({ dbSchema });
|
||||
|
||||
console.log(newTable);
|
||||
|
||||
fields = await varDatabaseDbHandler({
|
||||
queryString: `SHOW COLUMNS FROM users`,
|
||||
database: dbFullName,
|
||||
});
|
||||
}
|
||||
|
||||
if (!fields) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Could not create users table",
|
||||
};
|
||||
}
|
||||
|
||||
const fieldsTitles = fields.map(
|
||||
(/** @type {*} */ fieldObject) => fieldObject.Field
|
||||
);
|
||||
|
||||
let invalidField = null;
|
||||
|
||||
for (let i = 0; i < Object.keys(payload).length; i++) {
|
||||
const key = Object.keys(payload)[i];
|
||||
if (!fieldsTitles.includes(key)) {
|
||||
invalidField = key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (invalidField) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `${invalidField} is not a valid field!`,
|
||||
};
|
||||
}
|
||||
|
||||
if (!dbSchema) {
|
||||
throw new Error("Db Schema not found!");
|
||||
}
|
||||
|
||||
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],
|
||||
database: dbFullName,
|
||||
tableSchema: tableSchema,
|
||||
});
|
||||
|
||||
if (existingUser && existingUser[0]) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "User Already Exists",
|
||||
};
|
||||
}
|
||||
|
||||
const addUser = await addDbEntry({
|
||||
dbFullName: dbFullName,
|
||||
tableName: "users",
|
||||
data: {
|
||||
...payload,
|
||||
image: "/images/user_images/user-preset.png",
|
||||
image_thumbnail:
|
||||
"/images/user_images/user-preset-thumbnail.png",
|
||||
},
|
||||
encryptionKey: encryptionKeyFinal,
|
||||
encryptionSalt: encryptionSaltFinal,
|
||||
tableSchema,
|
||||
});
|
||||
|
||||
if (addUser?.insertId) {
|
||||
const newlyAddedUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT id,first_name,last_name,email,username,phone,image,image_thumbnail,city,state,country,zip_code,address,verification_status,more_user_data FROM users WHERE id='${addUser.insertId}'`,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: newlyAddedUser?.[0],
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Could not create user",
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
////////////////////////////////////////
|
||||
console.log("Error in local add-user Request =>", error.message);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
msg: "Something went wrong!",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = localAddUser;
|
22
engine/user/get-user.d.ts
vendored
22
engine/user/get-user.d.ts
vendored
@ -1,22 +0,0 @@
|
||||
export = getLocalUser;
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {number} param0.userId
|
||||
* @param {string[]} param0.fields
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [param0.dbSchema]
|
||||
* @returns
|
||||
*/
|
||||
declare function getLocalUser({ userId, fields, dbSchema }: {
|
||||
userId: number;
|
||||
fields: string[];
|
||||
dbSchema?: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
}): Promise<{
|
||||
success: boolean;
|
||||
payload: any;
|
||||
msg: string;
|
||||
} | {
|
||||
success: boolean;
|
||||
payload: any;
|
||||
msg?: undefined;
|
||||
}>;
|
@ -1,55 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {number} param0.userId
|
||||
* @param {string[]} param0.fields
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [param0.dbSchema]
|
||||
* @returns
|
||||
*/
|
||||
async function getLocalUser({ userId, fields, dbSchema }) {
|
||||
/**
|
||||
* GRAB user
|
||||
*
|
||||
* @description GRAB user
|
||||
*/
|
||||
const sanitizedFields = fields.map((fld) => fld.replace(/[^a-z\_]/g, ""));
|
||||
const query = `SELECT ${sanitizedFields.join(",")} FROM users WHERE id = ?`;
|
||||
|
||||
const tableSchema = dbSchema?.tables.find(
|
||||
(tb) => tb?.tableName === "users"
|
||||
);
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: query,
|
||||
queryValuesArray: [userId.toString()],
|
||||
database: process.env.DSQL_DB_NAME || "",
|
||||
tableSchema,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!foundUser || !foundUser[0])
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "User not found!",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** ********************* Send Response */
|
||||
return {
|
||||
success: true,
|
||||
payload: foundUser[0],
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = getLocalUser;
|
39
engine/user/login-user.d.ts
vendored
39
engine/user/login-user.d.ts
vendored
@ -1,39 +0,0 @@
|
||||
export = loginLocalUser;
|
||||
/**
|
||||
*
|
||||
* @param {import("../../package-shared/types").PackageUserLoginLocalBody} param0
|
||||
* @returns
|
||||
*/
|
||||
declare function loginLocalUser({ payload, additionalFields, dbSchema, email_login, email_login_code, email_login_field, }: import("../../package-shared/types").PackageUserLoginLocalBody): Promise<{
|
||||
success: boolean;
|
||||
msg: string;
|
||||
payload?: undefined;
|
||||
userId?: undefined;
|
||||
} | {
|
||||
success: boolean;
|
||||
payload: any;
|
||||
msg: string;
|
||||
userId?: undefined;
|
||||
} | {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
payload: {
|
||||
id: any;
|
||||
first_name: any;
|
||||
last_name: any;
|
||||
username: any;
|
||||
email: any;
|
||||
phone: any;
|
||||
social_id: any;
|
||||
image: any;
|
||||
image_thumbnail: any;
|
||||
verification_status: any;
|
||||
social_login: any;
|
||||
social_platform: any;
|
||||
csrf_k: string;
|
||||
more_data: any;
|
||||
logged_in_status: boolean;
|
||||
date: number;
|
||||
};
|
||||
userId: string;
|
||||
}>;
|
@ -1,188 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const hashPassword = require("../../functions/hashPassword");
|
||||
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {import("../../package-shared/types").PackageUserLoginLocalBody} param0
|
||||
* @returns
|
||||
*/
|
||||
async function loginLocalUser({
|
||||
payload,
|
||||
additionalFields,
|
||||
dbSchema,
|
||||
email_login,
|
||||
email_login_code,
|
||||
email_login_field,
|
||||
}) {
|
||||
try {
|
||||
/**
|
||||
* User auth
|
||||
*
|
||||
* @description Authenticate user
|
||||
*/
|
||||
|
||||
const email = payload.email;
|
||||
const username = payload.username;
|
||||
const password = payload.password;
|
||||
|
||||
const dbFullName = process.env.DSQL_DB_NAME || "";
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
*
|
||||
* @description Check input validity
|
||||
*/
|
||||
if (
|
||||
email?.match(/ /) ||
|
||||
(username && username?.match(/ /)) ||
|
||||
(password && password?.match(/ /))
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Invalid Email/Password format",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Password hash
|
||||
*
|
||||
* @description Password hash
|
||||
*/
|
||||
let hashedPassword = password
|
||||
? hashPassword({
|
||||
password: password,
|
||||
encryptionKey: encryptionKey,
|
||||
})
|
||||
: null;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const tableSchema = dbSchema?.tables.find(
|
||||
(tb) => tb?.tableName === "users"
|
||||
);
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM users WHERE email = ? OR username = ?`,
|
||||
queryValuesArray: [email || "", username || ""],
|
||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||
tableSchema,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!foundUser || !foundUser[0])
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "No user found",
|
||||
};
|
||||
|
||||
let isPasswordCorrect = false;
|
||||
|
||||
if (foundUser && foundUser[0] && !email_login) {
|
||||
isPasswordCorrect = hashedPassword === foundUser[0].password;
|
||||
} else if (
|
||||
foundUser &&
|
||||
foundUser[0] &&
|
||||
email_login &&
|
||||
email_login_code &&
|
||||
email_login_field
|
||||
) {
|
||||
const tempCode = foundUser[0][email_login_field];
|
||||
|
||||
if (!tempCode) throw new Error("No code Found!");
|
||||
|
||||
const tempCodeArray = tempCode.split("-");
|
||||
const [code, codeDate] = tempCodeArray;
|
||||
const millisecond15mins = 1000 * 60 * 15;
|
||||
|
||||
if (Date.now() - Number(codeDate) > millisecond15mins) {
|
||||
throw new Error("Code Expired");
|
||||
}
|
||||
isPasswordCorrect = code === email_login_code;
|
||||
}
|
||||
|
||||
let socialUserValid = false;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!isPasswordCorrect && !socialUserValid) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Wrong password, no social login validity",
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let csrfKey =
|
||||
Math.random().toString(36).substring(2) +
|
||||
"-" +
|
||||
Math.random().toString(36).substring(2);
|
||||
|
||||
let userPayload = {
|
||||
id: foundUser[0].id,
|
||||
first_name: foundUser[0].first_name,
|
||||
last_name: foundUser[0].last_name,
|
||||
username: foundUser[0].username,
|
||||
email: foundUser[0].email,
|
||||
phone: foundUser[0].phone,
|
||||
social_id: foundUser[0].social_id,
|
||||
image: foundUser[0].image,
|
||||
image_thumbnail: foundUser[0].image_thumbnail,
|
||||
verification_status: foundUser[0].verification_status,
|
||||
social_login: foundUser[0].social_login,
|
||||
social_platform: foundUser[0].social_platform,
|
||||
csrf_k: csrfKey,
|
||||
more_data: foundUser[0].more_user_data,
|
||||
logged_in_status: true,
|
||||
date: Date.now(),
|
||||
};
|
||||
|
||||
if (
|
||||
additionalFields &&
|
||||
Array.isArray(additionalFields) &&
|
||||
additionalFields.length > 0
|
||||
) {
|
||||
additionalFields.forEach((key) => {
|
||||
// @ts-ignore
|
||||
userPayload[key] = foundUser?.[0][key];
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** ********************* Send Response */
|
||||
return {
|
||||
success: true,
|
||||
msg: "Login Successful",
|
||||
payload: userPayload,
|
||||
userId: "0",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Error in local login-user Request =>", error.message);
|
||||
return {
|
||||
success: false,
|
||||
msg: "Login Failed",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = loginLocalUser;
|
46
engine/user/reauth-user.d.ts
vendored
46
engine/user/reauth-user.d.ts
vendored
@ -1,46 +0,0 @@
|
||||
export = localReauthUser;
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {*} param0.existingUser
|
||||
* @param {string[]} [param0.additionalFields]
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [param0.dbSchema]
|
||||
* @returns
|
||||
*/
|
||||
declare function localReauthUser({ existingUser, additionalFields, dbSchema }: {
|
||||
existingUser: any;
|
||||
additionalFields?: string[];
|
||||
dbSchema?: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
}): Promise<{
|
||||
success: boolean;
|
||||
payload: any;
|
||||
msg: string;
|
||||
userId?: undefined;
|
||||
} | {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
payload: {
|
||||
id: any;
|
||||
first_name: any;
|
||||
last_name: any;
|
||||
username: any;
|
||||
email: any;
|
||||
phone: any;
|
||||
social_id: any;
|
||||
image: any;
|
||||
image_thumbnail: any;
|
||||
verification_status: any;
|
||||
social_login: any;
|
||||
social_platform: any;
|
||||
csrf_k: string;
|
||||
more_data: any;
|
||||
logged_in_status: boolean;
|
||||
date: number;
|
||||
};
|
||||
userId: string;
|
||||
} | {
|
||||
success: boolean;
|
||||
msg: string;
|
||||
payload?: undefined;
|
||||
userId?: undefined;
|
||||
}>;
|
@ -1,115 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {*} param0.existingUser
|
||||
* @param {string[]} [param0.additionalFields]
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [param0.dbSchema]
|
||||
* @returns
|
||||
*/
|
||||
async function localReauthUser({ existingUser, additionalFields, dbSchema }) {
|
||||
try {
|
||||
/**
|
||||
* Grab data
|
||||
*
|
||||
* @description Grab data
|
||||
*/
|
||||
const dbFullName = process.env.DSQL_DB_NAME || "";
|
||||
|
||||
/**
|
||||
* GRAB user
|
||||
*
|
||||
* @description GRAB user
|
||||
*/
|
||||
const tableSchema = dbSchema?.tables.find(
|
||||
(tb) => tb?.tableName === "users"
|
||||
);
|
||||
|
||||
let foundUser =
|
||||
existingUser?.id && existingUser.id.toString().match(/./)
|
||||
? await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM users WHERE id=?`,
|
||||
queryValuesArray: [existingUser.id],
|
||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||
tableSchema,
|
||||
})
|
||||
: null;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!foundUser || !foundUser[0])
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "No user found",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let csrfKey =
|
||||
Math.random().toString(36).substring(2) +
|
||||
"-" +
|
||||
Math.random().toString(36).substring(2);
|
||||
|
||||
let userPayload = {
|
||||
id: foundUser[0].id,
|
||||
first_name: foundUser[0].first_name,
|
||||
last_name: foundUser[0].last_name,
|
||||
username: foundUser[0].username,
|
||||
email: foundUser[0].email,
|
||||
phone: foundUser[0].phone,
|
||||
social_id: foundUser[0].social_id,
|
||||
image: foundUser[0].image,
|
||||
image_thumbnail: foundUser[0].image_thumbnail,
|
||||
verification_status: foundUser[0].verification_status,
|
||||
social_login: foundUser[0].social_login,
|
||||
social_platform: foundUser[0].social_platform,
|
||||
csrf_k: csrfKey,
|
||||
more_data: foundUser[0].more_user_data,
|
||||
logged_in_status: true,
|
||||
date: Date.now(),
|
||||
};
|
||||
|
||||
if (
|
||||
additionalFields &&
|
||||
Array.isArray(additionalFields) &&
|
||||
additionalFields.length > 0
|
||||
) {
|
||||
additionalFields.forEach((key) => {
|
||||
// @ts-ignore
|
||||
userPayload[key] = foundUser?.[0][key];
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** ********************* Send Response */
|
||||
return {
|
||||
success: true,
|
||||
msg: "Login Successful",
|
||||
payload: userPayload,
|
||||
userId: "0",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Error in local login-user Request =>", error.message);
|
||||
return {
|
||||
success: false,
|
||||
msg: "Login Failed",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = localReauthUser;
|
32
engine/user/send-email-code.d.ts
vendored
32
engine/user/send-email-code.d.ts
vendored
@ -1,32 +0,0 @@
|
||||
export = localSendEmailCode;
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.email
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [param0.dbSchema]
|
||||
* @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
|
||||
*/
|
||||
declare function localSendEmailCode({ email, dbSchema, email_login_field, mail_domain, mail_username, mail_password, mail_port, sender, }: {
|
||||
email: string;
|
||||
dbSchema?: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
email_login_field: string;
|
||||
mail_domain?: string;
|
||||
mail_username?: string;
|
||||
mail_password?: string;
|
||||
mail_port?: number;
|
||||
sender?: string;
|
||||
}): Promise<{
|
||||
success: boolean;
|
||||
msg: string;
|
||||
payload?: undefined;
|
||||
} | {
|
||||
success: boolean;
|
||||
payload: any;
|
||||
msg: string;
|
||||
}>;
|
@ -1,153 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const hashPassword = require("../../functions/hashPassword");
|
||||
const varDatabaseDbHandler = require("../engine/utils/varDatabaseDbHandler");
|
||||
const nodemailer = require("nodemailer");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.email
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} [param0.dbSchema]
|
||||
* @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
|
||||
*/
|
||||
async function localSendEmailCode({
|
||||
email,
|
||||
dbSchema,
|
||||
email_login_field,
|
||||
mail_domain,
|
||||
mail_username,
|
||||
mail_password,
|
||||
mail_port,
|
||||
sender,
|
||||
}) {
|
||||
try {
|
||||
/**
|
||||
* User auth
|
||||
*
|
||||
* @description Authenticate user
|
||||
*/
|
||||
const dbFullName = process.env.DSQL_DB_NAME || "";
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
*
|
||||
* @description Check input validity
|
||||
*/
|
||||
if (email?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Invalid Email/Password format",
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const tableSchema = dbSchema?.tables.find(
|
||||
(tb) => tb?.tableName === "users"
|
||||
);
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM users WHERE email = ?`,
|
||||
queryValuesArray: [email],
|
||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||
tableSchema,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!foundUser || !foundUser[0])
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "No user found",
|
||||
};
|
||||
|
||||
if (foundUser && foundUser[0] && email_login_field) {
|
||||
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@datasquirel.com";
|
||||
mailObject["to"] = email;
|
||||
mailObject["subject"] = "One Time 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({
|
||||
queryString: `UPDATE users SET ${email_login_field} = ? WHERE email = ?`,
|
||||
queryValuesArray: [tempCode + `-${Date.now()}`, email],
|
||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||
tableSchema,
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** ********************* Send Response */
|
||||
return {
|
||||
success: true,
|
||||
msg: "Success",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("Error in local login-user Request =>", error.message);
|
||||
return {
|
||||
success: false,
|
||||
msg: "Failed: " + error.message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function generateCode() {
|
||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
let code = "";
|
||||
for (let i = 0; i < 8; i++) {
|
||||
code += chars[Math.floor(Math.random() * chars.length)];
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
module.exports = localSendEmailCode;
|
71
engine/user/social/github-auth.d.ts
vendored
71
engine/user/social/github-auth.d.ts
vendored
@ -1,71 +0,0 @@
|
||||
export = localGithubAuth;
|
||||
/**
|
||||
* SERVER FUNCTION: Login with google Function
|
||||
* ==============================================================================
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {http.ServerResponse} params.res - HTTPS response object
|
||||
* @param {string} params.code
|
||||
* @param {string} [params.email]
|
||||
* @param {string} params.clientId
|
||||
* @param {string} params.clientSecret
|
||||
* @param {object} [params.additionalFields]
|
||||
* @param {import("../../../package-shared/types").DSQL_DatabaseSchemaType} params.dbSchema
|
||||
*/
|
||||
declare function localGithubAuth({ res, code, email, clientId, clientSecret, additionalFields, dbSchema, }: {
|
||||
res: http.ServerResponse;
|
||||
code: string;
|
||||
email?: string;
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
additionalFields?: object;
|
||||
dbSchema: import("../../../package-shared/types").DSQL_DatabaseSchemaType;
|
||||
}): Promise<{
|
||||
success: boolean;
|
||||
msg: string;
|
||||
} | {
|
||||
dsqlUserId: string;
|
||||
/**
|
||||
* - Did the operation complete successfully or not?
|
||||
*/
|
||||
success: boolean;
|
||||
/**
|
||||
* - User payload object: or "null"
|
||||
*/
|
||||
user: {
|
||||
id: number;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
} | null;
|
||||
/**
|
||||
* - Message
|
||||
*/
|
||||
msg?: string;
|
||||
/**
|
||||
* - Error Message
|
||||
*/
|
||||
error?: string;
|
||||
/**
|
||||
* - Social Id
|
||||
*/
|
||||
social_id?: string | number;
|
||||
/**
|
||||
* - Social Platform
|
||||
*/
|
||||
social_platform?: string;
|
||||
/**
|
||||
* - Payload
|
||||
*/
|
||||
payload?: object;
|
||||
/**
|
||||
* - Alert
|
||||
*/
|
||||
alert?: boolean;
|
||||
/**
|
||||
* - New User
|
||||
*/
|
||||
newUser?: any;
|
||||
}>;
|
||||
import http = require("http");
|
@ -1,151 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
const http = require("http");
|
||||
const https = require("https");
|
||||
const encrypt = require("../../../functions/encrypt");
|
||||
const camelJoinedtoCamelSpace = require("../../engine/utils/camelJoinedtoCamelSpace");
|
||||
const githubLogin = require("./utils/githubLogin");
|
||||
const handleSocialDb = require("./utils/handleSocialDb");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
const database = process.env.DSQL_DB_NAME || "";
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
/**
|
||||
* SERVER FUNCTION: Login with google Function
|
||||
* ==============================================================================
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {http.ServerResponse} params.res - HTTPS response object
|
||||
* @param {string} params.code
|
||||
* @param {string} [params.email]
|
||||
* @param {string} params.clientId
|
||||
* @param {string} params.clientSecret
|
||||
* @param {object} [params.additionalFields]
|
||||
* @param {import("../../../package-shared/types").DSQL_DatabaseSchemaType} params.dbSchema
|
||||
*/
|
||||
async function localGithubAuth({
|
||||
res,
|
||||
code,
|
||||
email,
|
||||
clientId,
|
||||
clientSecret,
|
||||
additionalFields,
|
||||
dbSchema,
|
||||
}) {
|
||||
try {
|
||||
/**
|
||||
* User auth
|
||||
*
|
||||
* @description Authenticate user
|
||||
*/
|
||||
if (!code || !clientId || !clientSecret) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Missing query params",
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
typeof code !== "string" ||
|
||||
typeof clientId !== "string" ||
|
||||
typeof clientSecret !== "string" ||
|
||||
typeof database !== "string"
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Wrong Parameters",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
const gitHubUser = await githubLogin({
|
||||
code: code,
|
||||
clientId: clientId,
|
||||
clientSecret: clientSecret,
|
||||
});
|
||||
|
||||
if (!gitHubUser) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "No github user returned",
|
||||
};
|
||||
}
|
||||
|
||||
const targetDbName = database;
|
||||
|
||||
const socialId = gitHubUser.name || gitHubUser.id || gitHubUser.login;
|
||||
const targetName = gitHubUser.name || gitHubUser.login;
|
||||
const nameArray = targetName?.match(/ /)
|
||||
? targetName?.split(" ")
|
||||
: targetName?.match(/\-/)
|
||||
? targetName?.split("-")
|
||||
: [targetName];
|
||||
|
||||
const payload = {
|
||||
email: gitHubUser.email,
|
||||
first_name: camelJoinedtoCamelSpace(nameArray[0]) || "",
|
||||
last_name: camelJoinedtoCamelSpace(nameArray[1]) || "",
|
||||
social_id: socialId,
|
||||
social_platform: "github",
|
||||
image: gitHubUser.avatar_url,
|
||||
image_thumbnail: gitHubUser.avatar_url,
|
||||
username: "github-user-" + socialId,
|
||||
};
|
||||
|
||||
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
||||
Object.keys(additionalFields).forEach((key) => {
|
||||
// @ts-ignore
|
||||
payload[key] = additionalFields[key];
|
||||
});
|
||||
}
|
||||
|
||||
const loggedInGithubUser = await handleSocialDb({
|
||||
database: targetDbName,
|
||||
email: gitHubUser.email,
|
||||
payload: payload,
|
||||
social_platform: "github",
|
||||
res: res,
|
||||
social_id: socialId,
|
||||
supEmail: email,
|
||||
additionalFields,
|
||||
dbSchema: dbSchema,
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return { ...loggedInGithubUser, dsqlUserId: "0" };
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
console.log("localGithubAuth error", error.message);
|
||||
|
||||
return { success: false, msg: "Failed!" };
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
module.exports = localGithubAuth;
|
28
engine/user/social/google-auth.d.ts
vendored
28
engine/user/social/google-auth.d.ts
vendored
@ -1,28 +0,0 @@
|
||||
export = localGoogleAuth;
|
||||
/**
|
||||
* SERVER FUNCTION: Login with google Function
|
||||
* ==============================================================================
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {string} params.token - Google access token gotten from the client side
|
||||
* @param {string} params.clientId - Google client id
|
||||
* @param {http.ServerResponse} params.response - HTTPS response object
|
||||
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {import("../../../package-shared/types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database Schema
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> }
|
||||
*/
|
||||
declare function localGoogleAuth({ dbSchema, token, clientId, response, additionalFields, }: {
|
||||
token: string;
|
||||
clientId: string;
|
||||
response: http.ServerResponse;
|
||||
additionalFields?: object;
|
||||
dbSchema?: import("../../../package-shared/types").DSQL_DatabaseSchemaType;
|
||||
}): Promise<FunctionReturn>;
|
||||
declare namespace localGoogleAuth {
|
||||
export { FunctionReturn };
|
||||
}
|
||||
import http = require("http");
|
||||
type FunctionReturn = object | null;
|
@ -1,174 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
const http = require("http");
|
||||
const handleSocialDb = require("./utils/handleSocialDb");
|
||||
const httpsRequest = require("./utils/httpsRequest");
|
||||
const grabHostNames = require("../../../package-shared/utils/grab-host-names");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* @typedef {object | null} FunctionReturn
|
||||
* @property {boolean} success - Did the function run successfully?
|
||||
* @property {import("../../types/user.td").DATASQUIREL_LoggedInUser | null} user - Returned User
|
||||
* @property {number} [dsqlUserId] - Dsql User Id
|
||||
* @property {string} [msg] - Response message
|
||||
*/
|
||||
|
||||
const database = process.env.DSQL_DB_NAME || "";
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
/**
|
||||
* SERVER FUNCTION: Login with google Function
|
||||
* ==============================================================================
|
||||
*
|
||||
* @async
|
||||
*
|
||||
* @param {object} params - main params object
|
||||
* @param {string} params.token - Google access token gotten from the client side
|
||||
* @param {string} params.clientId - Google client id
|
||||
* @param {http.ServerResponse} params.response - HTTPS response object
|
||||
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
|
||||
* @param {import("../../../package-shared/types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database Schema
|
||||
*
|
||||
* @returns { Promise<FunctionReturn> }
|
||||
*/
|
||||
async function localGoogleAuth({
|
||||
dbSchema,
|
||||
token,
|
||||
clientId,
|
||||
response,
|
||||
additionalFields,
|
||||
}) {
|
||||
/**
|
||||
* Send Response
|
||||
*
|
||||
* @description Send a boolean response
|
||||
*/
|
||||
const { host, port, scheme } = grabHostNames();
|
||||
|
||||
try {
|
||||
/**
|
||||
* Grab User data
|
||||
*
|
||||
* @description Grab User data
|
||||
* @type {{ success: boolean, payload: any, msg: string }}
|
||||
*/
|
||||
const payloadResponse = await httpsRequest({
|
||||
method: "POST",
|
||||
hostname: host,
|
||||
path: "/user/grab-google-user-from-token",
|
||||
body: {
|
||||
token: token,
|
||||
clientId: clientId,
|
||||
},
|
||||
headers: {
|
||||
Authorization: process.env.DSQL_API_KEY,
|
||||
},
|
||||
});
|
||||
|
||||
const payload = payloadResponse.payload;
|
||||
|
||||
if (!payloadResponse.success || !payload) {
|
||||
console.log("payloadResponse Failed =>", payloadResponse);
|
||||
return {
|
||||
success: false,
|
||||
msg: "User fetch Error",
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!database || typeof database != "string" || database?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
user: undefined,
|
||||
msg: "Please provide a database slug(database name in lowercase with no spaces)",
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
const targetDbName = database;
|
||||
|
||||
if (!payload) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "No payload",
|
||||
};
|
||||
}
|
||||
|
||||
const { given_name, family_name, email, sub, picture, email_verified } =
|
||||
payload;
|
||||
|
||||
const payloadObject = {
|
||||
email: email || "",
|
||||
first_name: given_name || "",
|
||||
last_name: family_name || "",
|
||||
social_id: sub,
|
||||
social_platform: "google",
|
||||
image: picture || "",
|
||||
image_thumbnail: picture || "",
|
||||
username: `google-user-${sub}`,
|
||||
};
|
||||
|
||||
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
||||
Object.keys(additionalFields).forEach((key) => {
|
||||
// @ts-ignore
|
||||
payloadObject[key] = additionalFields[key];
|
||||
});
|
||||
}
|
||||
|
||||
const loggedInGoogleUser = await handleSocialDb({
|
||||
database: targetDbName,
|
||||
email: email || "",
|
||||
payload: payloadObject,
|
||||
social_platform: "google",
|
||||
res: response,
|
||||
social_id: sub,
|
||||
additionalFields,
|
||||
dbSchema,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return { ...loggedInGoogleUser, dsqlUserId: "0" };
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (error) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "User fetch Error",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
module.exports = localGoogleAuth;
|
182
engine/user/social/utils/githubLogin.d.ts
vendored
182
engine/user/social/utils/githubLogin.d.ts
vendored
@ -1,182 +0,0 @@
|
||||
export = githubLogin;
|
||||
/**
|
||||
*
|
||||
* @typedef {object} GithubUserPayload
|
||||
* @property {string} login - Full name merged eg. "JohnDoe"
|
||||
* @property {number} id - github user id
|
||||
* @property {string} node_id - Some other id
|
||||
* @property {string} avatar_url - profile picture
|
||||
* @property {string} gravatar_id - some other id
|
||||
* @property {string} url - Github user URL
|
||||
* @property {string} html_url - User html URL - whatever that means
|
||||
* @property {string} followers_url - Followers URL
|
||||
* @property {string} following_url - Following URL
|
||||
* @property {string} gists_url - Gists URL
|
||||
* @property {string} starred_url - Starred URL
|
||||
* @property {string} subscriptions_url - Subscriptions URL
|
||||
* @property {string} organizations_url - Organizations URL
|
||||
* @property {string} repos_url - Repositories URL
|
||||
* @property {string} received_events_url - Received Events URL
|
||||
* @property {string} type - Common value => "User"
|
||||
* @property {boolean} site_admin - Is site admin or not? Boolean
|
||||
* @property {string} name - More like "username"
|
||||
* @property {string} company - User company
|
||||
* @property {string} blog - User blog URL
|
||||
* @property {string} location - User Location
|
||||
* @property {string} email - User Email
|
||||
* @property {string} hireable - Is user hireable
|
||||
* @property {string} bio - User bio
|
||||
* @property {string} twitter_username - User twitter username
|
||||
* @property {number} public_repos - Number of public repositories
|
||||
* @property {number} public_gists - Number of public gists
|
||||
* @property {number} followers - Number of followers
|
||||
* @property {number} following - Number of following
|
||||
* @property {string} created_at - Date created
|
||||
* @property {string} updated_at - Date updated
|
||||
*/
|
||||
/**
|
||||
* Login/signup a github user
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - foundUser if any
|
||||
* @param {string} params.code - github auth token
|
||||
* @param {string} params.clientId - github client Id
|
||||
* @param {string} params.clientSecret - github client Secret
|
||||
*
|
||||
* @returns {Promise<GithubUserPayload|null>}
|
||||
*/
|
||||
declare function githubLogin({ code, clientId, clientSecret }: {
|
||||
code: string;
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
}): Promise<GithubUserPayload | null>;
|
||||
declare namespace githubLogin {
|
||||
export { GithubUserPayload };
|
||||
}
|
||||
type GithubUserPayload = {
|
||||
/**
|
||||
* - Full name merged eg. "JohnDoe"
|
||||
*/
|
||||
login: string;
|
||||
/**
|
||||
* - github user id
|
||||
*/
|
||||
id: number;
|
||||
/**
|
||||
* - Some other id
|
||||
*/
|
||||
node_id: string;
|
||||
/**
|
||||
* - profile picture
|
||||
*/
|
||||
avatar_url: string;
|
||||
/**
|
||||
* - some other id
|
||||
*/
|
||||
gravatar_id: string;
|
||||
/**
|
||||
* - Github user URL
|
||||
*/
|
||||
url: string;
|
||||
/**
|
||||
* - User html URL - whatever that means
|
||||
*/
|
||||
html_url: string;
|
||||
/**
|
||||
* - Followers URL
|
||||
*/
|
||||
followers_url: string;
|
||||
/**
|
||||
* - Following URL
|
||||
*/
|
||||
following_url: string;
|
||||
/**
|
||||
* - Gists URL
|
||||
*/
|
||||
gists_url: string;
|
||||
/**
|
||||
* - Starred URL
|
||||
*/
|
||||
starred_url: string;
|
||||
/**
|
||||
* - Subscriptions URL
|
||||
*/
|
||||
subscriptions_url: string;
|
||||
/**
|
||||
* - Organizations URL
|
||||
*/
|
||||
organizations_url: string;
|
||||
/**
|
||||
* - Repositories URL
|
||||
*/
|
||||
repos_url: string;
|
||||
/**
|
||||
* - Received Events URL
|
||||
*/
|
||||
received_events_url: string;
|
||||
/**
|
||||
* - Common value => "User"
|
||||
*/
|
||||
type: string;
|
||||
/**
|
||||
* - Is site admin or not? Boolean
|
||||
*/
|
||||
site_admin: boolean;
|
||||
/**
|
||||
* - More like "username"
|
||||
*/
|
||||
name: string;
|
||||
/**
|
||||
* - User company
|
||||
*/
|
||||
company: string;
|
||||
/**
|
||||
* - User blog URL
|
||||
*/
|
||||
blog: string;
|
||||
/**
|
||||
* - User Location
|
||||
*/
|
||||
location: string;
|
||||
/**
|
||||
* - User Email
|
||||
*/
|
||||
email: string;
|
||||
/**
|
||||
* - Is user hireable
|
||||
*/
|
||||
hireable: string;
|
||||
/**
|
||||
* - User bio
|
||||
*/
|
||||
bio: string;
|
||||
/**
|
||||
* - User twitter username
|
||||
*/
|
||||
twitter_username: string;
|
||||
/**
|
||||
* - Number of public repositories
|
||||
*/
|
||||
public_repos: number;
|
||||
/**
|
||||
* - Number of public gists
|
||||
*/
|
||||
public_gists: number;
|
||||
/**
|
||||
* - Number of followers
|
||||
*/
|
||||
followers: number;
|
||||
/**
|
||||
* - Number of following
|
||||
*/
|
||||
following: number;
|
||||
/**
|
||||
* - Date created
|
||||
*/
|
||||
created_at: string;
|
||||
/**
|
||||
* - Date updated
|
||||
*/
|
||||
updated_at: string;
|
||||
};
|
99
engine/user/social/utils/handleSocialDb.d.ts
vendored
99
engine/user/social/utils/handleSocialDb.d.ts
vendored
@ -1,99 +0,0 @@
|
||||
export = handleSocialDb;
|
||||
/**
|
||||
* Handle Social User Auth on Datasquirel Database
|
||||
* ==============================================================================
|
||||
*
|
||||
* @description This function handles all social login logic after the social user
|
||||
* has been authenticated and userpayload is present. The payload MUST contain the
|
||||
* specified fields because this funciton will create a new user if the authenticated
|
||||
* user does not exist.
|
||||
*
|
||||
* @param {{
|
||||
* database: string|null|undefined,
|
||||
* social_id: string|number,
|
||||
* email: string,
|
||||
* social_platform: string,
|
||||
* payload: {
|
||||
* social_id: string | number,
|
||||
* email: string,
|
||||
* social_platform: string,
|
||||
* first_name: string,
|
||||
* last_name: string,
|
||||
* image: string,
|
||||
* image_thumbnail: string,
|
||||
* username: string,
|
||||
* },
|
||||
* res: http.ServerResponse,
|
||||
* supEmail?: string | null,
|
||||
* additionalFields?: object,
|
||||
* dbSchema: import("../../../../package-shared/types").DSQL_DatabaseSchemaType | undefined
|
||||
* }} params - function parameters inside an object
|
||||
*
|
||||
* @returns {Promise<FunctionReturn>} - Response object
|
||||
*/
|
||||
declare function handleSocialDb({ social_id, email, social_platform, payload, res, supEmail, additionalFields, dbSchema, }: {
|
||||
database: string | null | undefined;
|
||||
social_id: string | number;
|
||||
email: string;
|
||||
social_platform: string;
|
||||
payload: {
|
||||
social_id: string | number;
|
||||
email: string;
|
||||
social_platform: string;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
image: string;
|
||||
image_thumbnail: string;
|
||||
username: string;
|
||||
};
|
||||
res: http.ServerResponse;
|
||||
supEmail?: string | null;
|
||||
additionalFields?: object;
|
||||
dbSchema: import("../../../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
}): Promise<FunctionReturn>;
|
||||
declare namespace handleSocialDb {
|
||||
export { FunctionReturn };
|
||||
}
|
||||
import http = require("http");
|
||||
type FunctionReturn = {
|
||||
/**
|
||||
* - Did the operation complete successfully or not?
|
||||
*/
|
||||
success: boolean;
|
||||
/**
|
||||
* - User payload object: or "null"
|
||||
*/
|
||||
user: {
|
||||
id: number;
|
||||
first_name: string;
|
||||
last_name: string;
|
||||
} | null;
|
||||
/**
|
||||
* - Message
|
||||
*/
|
||||
msg?: string;
|
||||
/**
|
||||
* - Error Message
|
||||
*/
|
||||
error?: string;
|
||||
/**
|
||||
* - Social Id
|
||||
*/
|
||||
social_id?: string | number;
|
||||
/**
|
||||
* - Social Platform
|
||||
*/
|
||||
social_platform?: string;
|
||||
/**
|
||||
* - Payload
|
||||
*/
|
||||
payload?: object;
|
||||
/**
|
||||
* - Alert
|
||||
*/
|
||||
alert?: boolean;
|
||||
/**
|
||||
* - New User
|
||||
*/
|
||||
newUser?: any;
|
||||
};
|
23
engine/user/social/utils/httpsRequest.d.ts
vendored
23
engine/user/social/utils/httpsRequest.d.ts
vendored
@ -1,23 +0,0 @@
|
||||
export = httpsRequest;
|
||||
/**
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @param {{
|
||||
* url?: string,
|
||||
* method: string,
|
||||
* hostname: string,
|
||||
* path?: string,
|
||||
* href?: string,
|
||||
* headers?: object,
|
||||
* body?: object,
|
||||
* }} params - params
|
||||
*/
|
||||
declare function httpsRequest({ url, method, hostname, path, href, headers, body }: {
|
||||
url?: string;
|
||||
method: string;
|
||||
hostname: string;
|
||||
path?: string;
|
||||
href?: string;
|
||||
headers?: object;
|
||||
body?: object;
|
||||
}): Promise<any>;
|
44
engine/user/update-user.d.ts
vendored
44
engine/user/update-user.d.ts
vendored
@ -1,44 +0,0 @@
|
||||
export = localUpdateUser;
|
||||
/**
|
||||
* @typedef {Object} LocalPostReturn
|
||||
* @property {boolean} success - Did the function run successfully?
|
||||
* @property {*} [payload] - GET request results
|
||||
* @property {string} [msg] - Message
|
||||
* @property {string} [error] - Error Message
|
||||
*/
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {*} params.payload - SQL Query
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} params.dbSchema - Name of the table to query
|
||||
*
|
||||
* @returns { Promise<LocalPostReturn> } - Return Object
|
||||
*/
|
||||
declare function localUpdateUser({ payload, dbSchema }: {
|
||||
payload: any;
|
||||
dbSchema: import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined;
|
||||
}): Promise<LocalPostReturn>;
|
||||
declare namespace localUpdateUser {
|
||||
export { LocalPostReturn };
|
||||
}
|
||||
type LocalPostReturn = {
|
||||
/**
|
||||
* - Did the function run successfully?
|
||||
*/
|
||||
success: boolean;
|
||||
/**
|
||||
* - GET request results
|
||||
*/
|
||||
payload?: any;
|
||||
/**
|
||||
* - Message
|
||||
*/
|
||||
msg?: string;
|
||||
/**
|
||||
* - Error Message
|
||||
*/
|
||||
error?: string;
|
||||
};
|
@ -1,91 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const updateDbEntry = require("../../package-shared/functions/backend/db/updateDbEntry");
|
||||
|
||||
/**
|
||||
* @typedef {Object} LocalPostReturn
|
||||
* @property {boolean} success - Did the function run successfully?
|
||||
* @property {*} [payload] - GET request results
|
||||
* @property {string} [msg] - Message
|
||||
* @property {string} [error] - Error Message
|
||||
*/
|
||||
|
||||
/**
|
||||
* Make a get request to Datasquirel API
|
||||
* ==============================================================================
|
||||
* @async
|
||||
*
|
||||
* @param {Object} params - Single object passed
|
||||
* @param {*} params.payload - SQL Query
|
||||
* @param {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined} params.dbSchema - Name of the table to query
|
||||
*
|
||||
* @returns { Promise<LocalPostReturn> } - Return Object
|
||||
*/
|
||||
async function localUpdateUser({ payload, dbSchema }) {
|
||||
try {
|
||||
/**
|
||||
* User auth
|
||||
*
|
||||
* @description Authenticate user
|
||||
*/
|
||||
/**
|
||||
* Initialize Variables
|
||||
*/
|
||||
const dbFullName = process.env.DSQL_DB_NAME || "";
|
||||
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
const data = (() => {
|
||||
const reqBodyKeys = Object.keys(payload);
|
||||
const finalData = {};
|
||||
|
||||
reqBodyKeys.forEach((key) => {
|
||||
if (key?.match(/^date_|^id$/)) return;
|
||||
// @ts-ignore
|
||||
finalData[key] = payload[key];
|
||||
});
|
||||
|
||||
return finalData;
|
||||
})();
|
||||
|
||||
if (!dbSchema) {
|
||||
throw new Error("Db Schema not found!");
|
||||
}
|
||||
|
||||
const tableSchema = dbSchema.tables.find(
|
||||
(tb) => tb?.tableName === "users"
|
||||
);
|
||||
|
||||
const updateUser = await updateDbEntry({
|
||||
dbContext: "Dsql User",
|
||||
paradigm: "Full Access",
|
||||
dbFullName: dbFullName,
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: payload.id,
|
||||
data: data,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
tableSchema,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: updateUser,
|
||||
};
|
||||
} catch (/** @type {*} */ error) {
|
||||
////////////////////////////////////////
|
||||
console.log("Error in local add-user Request =>", error.message);
|
||||
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "Something went wrong!",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = localUpdateUser;
|
14
functions/decrypt.d.ts
vendored
14
functions/decrypt.d.ts
vendored
@ -1,14 +0,0 @@
|
||||
export = decrypt;
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.encryptedString
|
||||
* @param {string} param0.encryptionKey
|
||||
* @param {string} param0.encryptionSalt
|
||||
* @returns
|
||||
*/
|
||||
declare function decrypt({ encryptedString, encryptionKey, encryptionSalt }: {
|
||||
encryptedString: string;
|
||||
encryptionKey: string;
|
||||
encryptionSalt: string;
|
||||
}): string;
|
14
functions/encrypt.d.ts
vendored
14
functions/encrypt.d.ts
vendored
@ -1,14 +0,0 @@
|
||||
export = encrypt;
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.data
|
||||
* @param {string} param0.encryptionKey
|
||||
* @param {string} param0.encryptionSalt
|
||||
* @returns {string | null}
|
||||
*/
|
||||
declare function encrypt({ data, encryptionKey, encryptionSalt }: {
|
||||
data: string;
|
||||
encryptionKey: string;
|
||||
encryptionSalt: string;
|
||||
}): string | null;
|
8
functions/sql/sql-generator.d.ts
vendored
8
functions/sql/sql-generator.d.ts
vendored
@ -1,8 +0,0 @@
|
||||
export = sqlGenerator;
|
||||
declare function sqlGenerator(Param0: {
|
||||
genObject?: import("../../package-shared/types").ServerQueryParam;
|
||||
tableName: string;
|
||||
}): {
|
||||
string: string;
|
||||
values: string[];
|
||||
} | undefined;
|
52
index.d.ts
vendored
52
index.d.ts
vendored
@ -1,52 +0,0 @@
|
||||
import get = require("./utils/get");
|
||||
import post = require("./utils/post");
|
||||
export namespace media {
|
||||
export { uploadImage };
|
||||
export { uploadFile };
|
||||
export { deleteFile };
|
||||
}
|
||||
export namespace user {
|
||||
export { createUser };
|
||||
export { loginUser };
|
||||
export { sendEmailCode };
|
||||
export { logoutUser };
|
||||
export { userAuth };
|
||||
export { reAuthUser };
|
||||
export { updateUser };
|
||||
export { getUser };
|
||||
export { getToken };
|
||||
export { validateToken };
|
||||
export namespace social {
|
||||
export { loginWithGoogle };
|
||||
export { loginWithGithub };
|
||||
}
|
||||
}
|
||||
import getSchema = require("./utils/get-schema");
|
||||
import sanitizeSql = require("./utils/functions/sanitizeSql");
|
||||
import datasquirelClient = require("./client");
|
||||
export namespace sql {
|
||||
export { sqlGenerator };
|
||||
export { sqlInsertGenerator };
|
||||
export { sqlDeleteGenerator };
|
||||
export { trimSql as trim };
|
||||
}
|
||||
import uploadImage = require("./utils/upload-image");
|
||||
import uploadFile = require("./utils/upload-file");
|
||||
import deleteFile = require("./utils/delete-file");
|
||||
import createUser = require("./users/add-user");
|
||||
import loginUser = require("./users/login-user");
|
||||
import sendEmailCode = require("./users/send-email-code");
|
||||
import logoutUser = require("./users/logout-user");
|
||||
import userAuth = require("./users/user-auth");
|
||||
import reAuthUser = require("./users/reauth-user");
|
||||
import updateUser = require("./users/update-user");
|
||||
import getUser = require("./users/get-user");
|
||||
import getToken = require("./users/get-token");
|
||||
import validateToken = require("./users/validate-token");
|
||||
import loginWithGoogle = require("./users/social/google-auth");
|
||||
import loginWithGithub = require("./users/social/github-auth");
|
||||
import sqlGenerator = require("./functions/sql/sql-generator");
|
||||
import sqlInsertGenerator = require("./functions/sql/sql-insert-generator");
|
||||
import sqlDeleteGenerator = require("./functions/sql/sql-delete-generator");
|
||||
import trimSql = require("./package-shared/utils/trim-sql");
|
||||
export { get, post, getSchema, sanitizeSql, datasquirelClient as client };
|
6
index.js
6
index.js
@ -28,9 +28,9 @@ const validateToken = require("./users/validate-token");
|
||||
|
||||
const sanitizeSql = require("./utils/functions/sanitizeSql");
|
||||
const datasquirelClient = require("./client");
|
||||
const sqlGenerator = require("./functions/sql/sql-generator");
|
||||
const sqlInsertGenerator = require("./functions/sql/sql-insert-generator");
|
||||
const sqlDeleteGenerator = require("./functions/sql/sql-delete-generator");
|
||||
const sqlGenerator = require("./package-shared/functions/dsql/sql/sql-generator");
|
||||
const sqlInsertGenerator = require("./package-shared/functions/dsql/sql/sql-insert-generator");
|
||||
const sqlDeleteGenerator = require("./package-shared/functions/dsql/sql/sql-delete-generator");
|
||||
const trimSql = require("./package-shared/utils/trim-sql");
|
||||
|
||||
////////////////////////////////////////
|
||||
|
1113
package-lock.json
generated
1113
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
6
engine/engine/data/dataTypes.json → package-shared/data/dataTypes.json
Normal file → Executable file
6
engine/engine/data/dataTypes.json → package-shared/data/dataTypes.json
Normal file → Executable file
@ -69,5 +69,11 @@
|
||||
"value": "0-255",
|
||||
"description": "LONGTEXT is just text with max length 4,294,967,295",
|
||||
"maxValue": 127
|
||||
},
|
||||
{
|
||||
"title": "UUID",
|
||||
"name": "UUID",
|
||||
"valueLiteral": "UUID()",
|
||||
"description": "A Unique ID"
|
||||
}
|
||||
]
|
16
engine/engine/data/defaultFields.json → package-shared/data/defaultFields.json
Normal file → Executable file
16
engine/engine/data/defaultFields.json → package-shared/data/defaultFields.json
Normal file → Executable file
@ -6,15 +6,20 @@
|
||||
"primaryKey": true,
|
||||
"autoIncrement": true
|
||||
},
|
||||
{
|
||||
"fieldName": "uuid",
|
||||
"dataType": "UUID",
|
||||
"defaultValueLiteral": "UUID()"
|
||||
},
|
||||
{
|
||||
"fieldName": "date_created",
|
||||
"dataType": "VARCHAR(250)",
|
||||
"notNullValue": true
|
||||
"nullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_created_code",
|
||||
"dataType": "BIGINT",
|
||||
"notNullValue": true
|
||||
"nullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_created_timestamp",
|
||||
@ -24,16 +29,17 @@
|
||||
{
|
||||
"fieldName": "date_updated",
|
||||
"dataType": "VARCHAR(250)",
|
||||
"notNullValue": true
|
||||
"nullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_updated_code",
|
||||
"dataType": "BIGINT",
|
||||
"notNullValue": true
|
||||
"nullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_updated_timestamp",
|
||||
"dataType": "TIMESTAMP",
|
||||
"defaultValueLiteral": "CURRENT_TIMESTAMP"
|
||||
"defaultValueLiteral": "CURRENT_TIMESTAMP",
|
||||
"onUpdateLiteral": "CURRENT_TIMESTAMP"
|
||||
}
|
||||
]
|
6
engine/engine/data/possibleFields.json → package-shared/data/possibleFields.json
Normal file → Executable file
6
engine/engine/data/possibleFields.json → package-shared/data/possibleFields.json
Normal file → Executable file
@ -13,5 +13,9 @@
|
||||
"destinationTableColumnName": "Column Name",
|
||||
"cascadeDelete": true,
|
||||
"cascadeUpdate": true
|
||||
}
|
||||
},
|
||||
"onUpdate": "CURRENT_TIMESTAMP",
|
||||
"onUpdateLiteral": "CURRENT_TIMESTAMP",
|
||||
"onDelete": "CURRENT_TIMESTAMP",
|
||||
"onDeleteLiteral": "CURRENT_TIMESTAMP"
|
||||
}
|
42
engine/engine/data/presets/users.json → package-shared/data/presets/users.json
Normal file → Executable file
42
engine/engine/data/presets/users.json → package-shared/data/presets/users.json
Normal file → Executable file
@ -2,13 +2,6 @@
|
||||
"tableName": "users",
|
||||
"tableFullName": "Users",
|
||||
"fields": [
|
||||
{
|
||||
"fieldName": "id",
|
||||
"dataType": "BIGINT",
|
||||
"notNullValue": true,
|
||||
"primaryKey": true,
|
||||
"autoIncrement": true
|
||||
},
|
||||
{
|
||||
"fieldName": "first_name",
|
||||
"dataType": "VARCHAR(100)",
|
||||
@ -46,12 +39,12 @@
|
||||
{
|
||||
"fieldName": "image",
|
||||
"dataType": "VARCHAR(250)",
|
||||
"defaultValue": "/images/user_images/user-preset.png"
|
||||
"defaultValue": "/images/user-preset.png"
|
||||
},
|
||||
{
|
||||
"fieldName": "image_thumbnail",
|
||||
"dataType": "VARCHAR(250)",
|
||||
"defaultValue": "/images/user_images/user-preset-thumbnail.png"
|
||||
"defaultValue": "/images/user-preset-thumbnail.png"
|
||||
},
|
||||
{
|
||||
"fieldName": "address",
|
||||
@ -99,34 +92,9 @@
|
||||
"defaultValue": "0"
|
||||
},
|
||||
{
|
||||
"fieldName": "date_created",
|
||||
"dataType": "VARCHAR(250)",
|
||||
"notNullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_created_code",
|
||||
"dataType": "BIGINT",
|
||||
"notNullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_created_timestamp",
|
||||
"dataType": "TIMESTAMP",
|
||||
"defaultValueLiteral": "CURRENT_TIMESTAMP"
|
||||
},
|
||||
{
|
||||
"fieldName": "date_updated",
|
||||
"dataType": "VARCHAR(250)",
|
||||
"notNullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_updated_code",
|
||||
"dataType": "BIGINT",
|
||||
"notNullValue": true
|
||||
},
|
||||
{
|
||||
"fieldName": "date_updated_timestamp",
|
||||
"dataType": "TIMESTAMP",
|
||||
"defaultValueLiteral": "CURRENT_TIMESTAMP"
|
||||
"fieldName": "temp_login_code",
|
||||
"dataType": "VARCHAR(50)",
|
||||
"nullValue": true
|
||||
}
|
||||
]
|
||||
}
|
93
package-shared/functions/api/query/get.js
Normal file
93
package-shared/functions/api/query/get.js
Normal file
@ -0,0 +1,93 @@
|
||||
// @ts-check
|
||||
|
||||
const _ = require("lodash");
|
||||
const serverError = require("../../backend/serverError");
|
||||
const runQuery = require("../../backend/db/runQuery");
|
||||
|
||||
/**
|
||||
* # Get Function FOr API
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.query
|
||||
* @param {(string|number)[]} [params.queryValues]
|
||||
* @param {string} params.dbFullName
|
||||
* @param {string} [params.tableName]
|
||||
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema]
|
||||
*
|
||||
* @returns {Promise<import("../../../types").GetReturn>}
|
||||
*/
|
||||
module.exports = async function apiGet({
|
||||
query,
|
||||
dbFullName,
|
||||
queryValues,
|
||||
tableName,
|
||||
dbSchema,
|
||||
}) {
|
||||
if (
|
||||
typeof query == "string" &&
|
||||
(query.match(/^alter|^delete|information_schema|databases|^create/i) ||
|
||||
!query.match(/^select/i))
|
||||
) {
|
||||
return { success: false, msg: "Wrong Input" };
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
let results;
|
||||
|
||||
try {
|
||||
let { result, error } = await runQuery({
|
||||
dbFullName: dbFullName,
|
||||
query: query,
|
||||
queryValuesArray: queryValues,
|
||||
readOnly: true,
|
||||
dbSchema,
|
||||
tableName,
|
||||
});
|
||||
|
||||
/** @type {import("../../../types").DSQL_TableSchemaType | undefined} */
|
||||
let tableSchema;
|
||||
|
||||
if (dbSchema) {
|
||||
const targetTable = dbSchema.tables.find(
|
||||
(table) => table.tableName === tableName
|
||||
);
|
||||
|
||||
if (targetTable) {
|
||||
const clonedTargetTable = _.cloneDeep(targetTable);
|
||||
delete clonedTargetTable.childTable;
|
||||
delete clonedTargetTable.childTableDbFullName;
|
||||
delete clonedTargetTable.childTableName;
|
||||
delete clonedTargetTable.childrenTables;
|
||||
delete clonedTargetTable.updateData;
|
||||
delete clonedTargetTable.tableNameOld;
|
||||
delete clonedTargetTable.indexes;
|
||||
tableSchema = clonedTargetTable;
|
||||
}
|
||||
}
|
||||
|
||||
if (error) throw error;
|
||||
if (result.error) throw new Error(result.error);
|
||||
|
||||
results = result;
|
||||
|
||||
/** @type {import("../../../types").GetReturn} */
|
||||
const resObject = {
|
||||
success: true,
|
||||
payload: results,
|
||||
schema: tableName && tableSchema ? tableSchema : undefined,
|
||||
};
|
||||
|
||||
return resObject;
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "/api/query/get/lines-85-94",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
return { success: false, payload: null, error: error.message };
|
||||
}
|
||||
};
|
100
package-shared/functions/api/query/post.js
Normal file
100
package-shared/functions/api/query/post.js
Normal file
@ -0,0 +1,100 @@
|
||||
// @ts-check
|
||||
|
||||
const _ = require("lodash");
|
||||
const serverError = require("../../backend/serverError");
|
||||
const runQuery = require("../../backend/db/runQuery");
|
||||
|
||||
/**
|
||||
* # Post Function For API
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {any} params.query
|
||||
* @param {(string|number)[]} [params.queryValues]
|
||||
* @param {string} params.dbFullName
|
||||
* @param {string} [params.tableName]
|
||||
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema]
|
||||
*
|
||||
* @returns {Promise<import("../../../types").PostReturn>}
|
||||
*/
|
||||
module.exports = async function apiPost({
|
||||
query,
|
||||
dbFullName,
|
||||
queryValues,
|
||||
tableName,
|
||||
dbSchema,
|
||||
}) {
|
||||
if (typeof query === "string" && query?.match(/^create |^alter |^drop /i)) {
|
||||
return { success: false, msg: "Wrong Input" };
|
||||
}
|
||||
|
||||
if (
|
||||
typeof query === "object" &&
|
||||
query?.action?.match(/^create |^alter |^drop /i)
|
||||
) {
|
||||
return { success: false, msg: "Wrong Input" };
|
||||
}
|
||||
|
||||
/** @type {any} */
|
||||
let results;
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
try {
|
||||
let { result, error } = await runQuery({
|
||||
dbFullName: dbFullName,
|
||||
query: query,
|
||||
dbSchema: dbSchema,
|
||||
queryValuesArray: queryValues,
|
||||
tableName,
|
||||
});
|
||||
|
||||
results = result;
|
||||
|
||||
if (error) throw error;
|
||||
|
||||
/** @type {import("../../../types").DSQL_TableSchemaType | undefined} */
|
||||
let tableSchema;
|
||||
|
||||
if (dbSchema) {
|
||||
const targetTable = dbSchema.tables.find(
|
||||
(table) => table.tableName === tableName
|
||||
);
|
||||
|
||||
if (targetTable) {
|
||||
const clonedTargetTable = _.cloneDeep(targetTable);
|
||||
|
||||
delete clonedTargetTable.childTable;
|
||||
delete clonedTargetTable.childTableDbFullName;
|
||||
delete clonedTargetTable.childTableName;
|
||||
delete clonedTargetTable.childrenTables;
|
||||
delete clonedTargetTable.updateData;
|
||||
delete clonedTargetTable.tableNameOld;
|
||||
delete clonedTargetTable.indexes;
|
||||
tableSchema = clonedTargetTable;
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: results,
|
||||
error: error,
|
||||
schema: tableName && tableSchema ? tableSchema : undefined,
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "/api/query/post/lines-132-142",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
return {
|
||||
success: false,
|
||||
payload: results,
|
||||
error: error.message,
|
||||
};
|
||||
}
|
||||
};
|
126
package-shared/functions/api/social-login/facebookLogin.js
Executable file
126
package-shared/functions/api/social-login/facebookLogin.js
Executable file
@ -0,0 +1,126 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
|
||||
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
||||
const handleNodemailer = require("../../backend/handleNodemailer");
|
||||
const { hashPassword } = require("../../backend/passwordHash");
|
||||
const serverError = require("../../backend/serverError");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - parameters object
|
||||
* @param {any} params.body
|
||||
* @param {import("../../../types").UserType} params.usertype
|
||||
*/
|
||||
module.exports = async function facebookLogin({ usertype, body }) {
|
||||
try {
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
const foundUser = await DB_HANDLER(
|
||||
`SELECT * FROM users WHERE email='${body.facebookUserEmail}' AND social_login='1'`
|
||||
);
|
||||
|
||||
if (foundUser && foundUser[0]) {
|
||||
return foundUser[0];
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
let socialHashedPassword = hashPassword(body.facebookUserId);
|
||||
|
||||
let newUser = await DB_HANDLER(`INSERT INTO ${usertype} (
|
||||
first_name,
|
||||
last_name,
|
||||
social_platform,
|
||||
social_name,
|
||||
email,
|
||||
image,
|
||||
image_thumbnail,
|
||||
password,
|
||||
verification_status,
|
||||
social_login,
|
||||
social_id,
|
||||
terms_agreement,
|
||||
date_created,
|
||||
date_code
|
||||
) VALUES (
|
||||
'${body.facebookUserFirstName}',
|
||||
'${body.facebookUserLastName}',
|
||||
'facebook',
|
||||
'facebook_${
|
||||
body.facebookUserEmail
|
||||
? body.facebookUserEmail.replace(/@.*/, "")
|
||||
: body.facebookUserFirstName.toLowerCase()
|
||||
}',
|
||||
'${body.facebookUserEmail}',
|
||||
'${body.facebookUserImage}',
|
||||
'${body.facebookUserImage}',
|
||||
'${socialHashedPassword}',
|
||||
'1',
|
||||
'1',
|
||||
'${body.facebookUserId}',
|
||||
'1',
|
||||
'${Date()}',
|
||||
'${Date.now()}'
|
||||
)`);
|
||||
|
||||
const newFoundUser = await DB_HANDLER(
|
||||
`SELECT * FROM ${usertype} WHERE id='${newUser.insertId}'`
|
||||
);
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Send email notifications to admin
|
||||
*
|
||||
* @description Send verification email to newly created agent
|
||||
*/
|
||||
// handleNodemailer({
|
||||
// to: "",
|
||||
// subject: "New Registered Buyer",
|
||||
// text: "We have a new registered Buyer from facebook",
|
||||
// html: `
|
||||
// <h2>${newFoundUser[0].first_name} ${newFoundUser[0].last_name} just registered from facebook.</h2>
|
||||
// <p>We have a new buyer registration</p>
|
||||
// <div>Name: <b>${newFoundUser[0].first_name} ${newFoundUser[0].last_name}</b></div>
|
||||
// <div>Email: <b>${newFoundUser[0].email}</b></div>
|
||||
// <div>Site: <b>${process.env.DSQL_HOST}</b></div>
|
||||
// `,
|
||||
// }).catch((error) => {
|
||||
// console.log(
|
||||
// "error in mail notification for new Facebook user =>",
|
||||
// error.message
|
||||
// );
|
||||
// });
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "functions/backend/facebookLogin",
|
||||
message: error.message,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
isFacebookAuthValid: false,
|
||||
newFoundUser: null,
|
||||
};
|
||||
};
|
50
engine/user/social/utils/githubLogin.js → package-shared/functions/api/social-login/githubLogin.js
Normal file → Executable file
50
engine/user/social/utils/githubLogin.js → package-shared/functions/api/social-login/githubLogin.js
Normal file → Executable file
@ -1,13 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
const fs = require("fs");
|
||||
const httpsRequest = require("./httpsRequest");
|
||||
const dbHandler = require("../../../engine/utils/dbHandler");
|
||||
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
||||
const httpsRequest = require("../../backend/httpsRequest");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
@ -62,11 +56,11 @@ const dbHandler = require("../../../engine/utils/dbHandler");
|
||||
* @param {string} params.clientId - github client Id
|
||||
* @param {string} params.clientSecret - github client Secret
|
||||
*
|
||||
* @returns {Promise<GithubUserPayload|null>}
|
||||
* @returns {Promise<GithubUserPayload|null|undefined>}
|
||||
*/
|
||||
async function githubLogin({ code, clientId, clientSecret }) {
|
||||
/** @type {GithubUserPayload | null} */
|
||||
let gitHubUser = null;
|
||||
module.exports = async function githubLogin({ code, clientId, clientSecret }) {
|
||||
/** @type {GithubUserPayload | undefined} */
|
||||
let gitHubUser;
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
@ -78,7 +72,7 @@ async function githubLogin({ code, clientId, clientSecret }) {
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
// const response = await fetch(`https://github.com/login/oauth/access_token?client_id=${process.env.GITHUB_ID}`);
|
||||
// const response = await fetch(`https://github.com/login/oauth/access_token?client_id=${process.env.DSQL_GITHUB_ID}`);
|
||||
const response = await httpsRequest({
|
||||
method: "POST",
|
||||
hostname: "github.com",
|
||||
@ -87,12 +81,13 @@ async function githubLogin({ code, clientId, clientSecret }) {
|
||||
Accept: "application/json",
|
||||
"User-Agent": "*",
|
||||
},
|
||||
scheme: "https",
|
||||
});
|
||||
|
||||
// `https://github.com/login/oauth/access_token?client_id=${process.env.GITHUB_ID}&client_secret=${process.env.GITHUB_SECRET}&code=${code}`,
|
||||
// `https://github.com/login/oauth/access_token?client_id=${process.env.DSQL_GITHUB_ID}&client_secret=${process.env.DSQL_GITHUB_SECRET}&code=${code}`,
|
||||
// body: JSON.stringify({
|
||||
// client_id: process.env.GITHUB_ID,
|
||||
// client_secret: process.env.GITHUB_SECRET,
|
||||
// client_id: process.env.DSQL_GITHUB_ID,
|
||||
// client_secret: process.env.DSQL_GITHUB_SECRET,
|
||||
// code: code,
|
||||
// }),
|
||||
|
||||
@ -114,6 +109,7 @@ async function githubLogin({ code, clientId, clientSecret }) {
|
||||
Authorization: `Bearer ${accessTokenObject.access_token}`,
|
||||
"User-Agent": "*",
|
||||
},
|
||||
scheme: "https",
|
||||
});
|
||||
|
||||
gitHubUser = JSON.parse(userDataResponse);
|
||||
@ -122,13 +118,12 @@ async function githubLogin({ code, clientId, clientSecret }) {
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
if (!gitHubUser?.email) {
|
||||
const existingGithubUser = await dbHandler({
|
||||
query: `SELECT email FROM users WHERE social_login='1' AND social_platform='github' AND social_id= ?`,
|
||||
values: [gitHubUser?.id || ""],
|
||||
});
|
||||
if (!gitHubUser?.email && gitHubUser) {
|
||||
const existingGithubUser = await DB_HANDLER(
|
||||
`SELECT email FROM users WHERE social_login='1' AND social_platform='github' AND social_id='${gitHubUser.id}'`
|
||||
);
|
||||
|
||||
if (existingGithubUser && existingGithubUser[0] && gitHubUser) {
|
||||
if (existingGithubUser && existingGithubUser[0]) {
|
||||
gitHubUser.email = existingGithubUser[0].email;
|
||||
}
|
||||
}
|
||||
@ -136,12 +131,15 @@ async function githubLogin({ code, clientId, clientSecret }) {
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
} catch (/** @type {any} */ error) {
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
console.log("ERROR in githubLogin.js backend function =>", error.message);
|
||||
console.log(
|
||||
"ERROR in githubLogin.js backend function =>",
|
||||
error.message
|
||||
);
|
||||
|
||||
// serverError({
|
||||
// component: "/api/social-login/github-auth/catch-error",
|
||||
@ -159,6 +157,4 @@ async function githubLogin({ code, clientId, clientSecret }) {
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return gitHubUser;
|
||||
}
|
||||
|
||||
module.exports = githubLogin;
|
||||
};
|
178
package-shared/functions/api/social-login/googleLogin.js
Executable file
178
package-shared/functions/api/social-login/googleLogin.js
Executable file
@ -0,0 +1,178 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
const fs = require("fs");
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
const { OAuth2Client } = require("google-auth-library");
|
||||
|
||||
const { hashPassword } = require("../../backend/passwordHash");
|
||||
const serverError = require("../../backend/serverError");
|
||||
const { ServerResponse } = require("http");
|
||||
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @param {Object} params
|
||||
* @param {string} params.usertype
|
||||
* @param {any} params.foundUser
|
||||
* @param {boolean} params.isSocialValidated
|
||||
* @param {boolean} params.isUserValid
|
||||
* @param {any} params.reqBody
|
||||
* @param {any} params.serverRes
|
||||
* @param {any} params.loginFailureReason
|
||||
*/
|
||||
module.exports = async function googleLogin({
|
||||
usertype,
|
||||
foundUser,
|
||||
isSocialValidated,
|
||||
isUserValid,
|
||||
reqBody,
|
||||
serverRes,
|
||||
loginFailureReason,
|
||||
}) {
|
||||
const client = new OAuth2Client(
|
||||
process.env.NEXT_PUBLIC_DSQL_GOOGLE_CLIENT_ID
|
||||
);
|
||||
let isGoogleAuthValid = false;
|
||||
let newFoundUser = null;
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
try {
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken: reqBody.token,
|
||||
audience: process.env.NEXT_PUBLIC_DSQL_GOOGLE_CLIENT_ID, // Specify the CLIENT_ID of the app that accesses the backend
|
||||
// Or, if multiple clients access the backend:
|
||||
//[CLIENT_ID_1, CLIENT_ID_2, CLIENT_ID_3]
|
||||
});
|
||||
|
||||
const payload = ticket.getPayload();
|
||||
const userid = payload?.["sub"];
|
||||
|
||||
if (!payload)
|
||||
throw new Error("Google login failed. Credentials invalid");
|
||||
|
||||
isUserValid = Boolean(payload.email_verified);
|
||||
|
||||
if (!isUserValid || !payload || !payload.email_verified) return;
|
||||
|
||||
serverRes.isUserValid = payload.email_verified;
|
||||
isSocialValidated = payload.email_verified;
|
||||
isGoogleAuthValid = payload.email_verified;
|
||||
////// If request specified a G Suite domain:
|
||||
////// const domain = payload['hd'];
|
||||
|
||||
let socialHashedPassword = hashPassword(payload.at_hash || "");
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
let existinEmail = await DB_HANDLER(
|
||||
`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login!='1' AND social_platform!='google'`
|
||||
);
|
||||
|
||||
if (existinEmail && existinEmail[0]) {
|
||||
loginFailureReason = "Email Exists Already";
|
||||
isGoogleAuthValid = false;
|
||||
return {
|
||||
isGoogleAuthValid: isGoogleAuthValid,
|
||||
newFoundUser: newFoundUser,
|
||||
loginFailureReason: loginFailureReason,
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
foundUser = await DB_HANDLER(
|
||||
`SELECT * FROM ${usertype} WHERE email='${payload.email}' AND social_login='1' AND social_platform='google'`
|
||||
);
|
||||
|
||||
if (foundUser && foundUser[0]) {
|
||||
newFoundUser = foundUser;
|
||||
return {
|
||||
isGoogleAuthValid: isGoogleAuthValid,
|
||||
newFoundUser: newFoundUser,
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
let newUser = await DB_HANDLER(`INSERT INTO ${usertype} (
|
||||
first_name,
|
||||
last_name,
|
||||
social_platform,
|
||||
social_name,
|
||||
social_id,
|
||||
email,
|
||||
image,
|
||||
image_thumbnail,
|
||||
password,
|
||||
verification_status,
|
||||
social_login,
|
||||
terms_agreement,
|
||||
date_created,
|
||||
date_code
|
||||
) VALUES (
|
||||
'${payload.given_name}',
|
||||
'${payload.family_name}',
|
||||
'google',
|
||||
'google_${payload.email?.replace(/@.*/, "")}',
|
||||
'${payload.sub}',
|
||||
'${payload.email}',
|
||||
'${payload.picture}',
|
||||
'${payload.picture}',
|
||||
'${socialHashedPassword}',
|
||||
'1',
|
||||
'1',
|
||||
'1',
|
||||
'${Date()}',
|
||||
'${Date.now()}'
|
||||
)`);
|
||||
|
||||
newFoundUser = await DB_HANDLER(
|
||||
`SELECT * FROM ${usertype} WHERE id='${newUser.insertId}'`
|
||||
);
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "googleLogin",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
loginFailureReason = error;
|
||||
|
||||
isUserValid = false;
|
||||
isSocialValidated = false;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return { isGoogleAuthValid: isGoogleAuthValid, newFoundUser: newFoundUser };
|
||||
};
|
219
engine/user/social/utils/handleSocialDb.js → package-shared/functions/api/social-login/handleSocialDb.js
Normal file → Executable file
219
engine/user/social/utils/handleSocialDb.js → package-shared/functions/api/social-login/handleSocialDb.js
Normal file → Executable file
@ -6,10 +6,20 @@
|
||||
* ==============================================================================
|
||||
*/
|
||||
const fs = require("fs");
|
||||
const http = require("http");
|
||||
const varDatabaseDbHandler = require("../../../engine/utils/varDatabaseDbHandler");
|
||||
const encrypt = require("../../../../functions/encrypt");
|
||||
const addDbEntry = require("../../../../package-shared/functions/backend/db/addDbEntry");
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
const addAdminUserOnLogin = require("../../backend/addAdminUserOnLogin");
|
||||
const handleNodemailer = require("../../backend/handleNodemailer");
|
||||
const { ServerResponse } = require("http");
|
||||
const path = require("path");
|
||||
const addMariadbUser = require("../../backend/addMariadbUser");
|
||||
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
||||
const encrypt = require("../../dsql/encrypt");
|
||||
const addDbEntry = require("../../backend/db/addDbEntry");
|
||||
const getAuthCookieNames = require("../../backend/cookies/get-auth-cookie-names");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
@ -26,72 +36,25 @@ const addDbEntry = require("../../../../package-shared/functions/backend/db/addD
|
||||
* first_name: string,
|
||||
* last_name: string,
|
||||
* }|null} user - User payload object: or "null"
|
||||
* @property {string} [msg] - Message
|
||||
* @property {string} [error] - Error Message
|
||||
* @property {string | number} [social_id] - Social Id
|
||||
* @property {string} [social_platform] - Social Platform
|
||||
* @property {object} [payload] - Payload
|
||||
* @property {boolean} [alert] - Alert
|
||||
* @property {*} [newUser] - New User
|
||||
*/
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
const database = process.env.DSQL_DB_NAME || "";
|
||||
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
||||
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* Handle Social User Auth on Datasquirel Database
|
||||
* ==============================================================================
|
||||
*
|
||||
* @description This function handles all social login logic after the social user
|
||||
* has been authenticated and userpayload is present. The payload MUST contain the
|
||||
* specified fields because this funciton will create a new user if the authenticated
|
||||
* user does not exist.
|
||||
*
|
||||
* @param {{
|
||||
* database: string|null|undefined,
|
||||
* social_id: string|number,
|
||||
* email: string,
|
||||
* social_platform: string,
|
||||
* payload: {
|
||||
* social_id: string | number,
|
||||
* email: string,
|
||||
* social_platform: string,
|
||||
* first_name: string,
|
||||
* last_name: string,
|
||||
* image: string,
|
||||
* image_thumbnail: string,
|
||||
* username: string,
|
||||
* },
|
||||
* res: http.ServerResponse,
|
||||
* supEmail?: string | null,
|
||||
* additionalFields?: object,
|
||||
* dbSchema: import("../../../../package-shared/types").DSQL_DatabaseSchemaType | undefined
|
||||
* }} params - function parameters inside an object
|
||||
*
|
||||
* @returns {Promise<FunctionReturn>} - Response object
|
||||
* @type {import("../../../types").HandleSocialDbFunction}
|
||||
*/
|
||||
async function handleSocialDb({
|
||||
module.exports = async function handleSocialDb({
|
||||
database,
|
||||
social_id,
|
||||
email,
|
||||
social_platform,
|
||||
payload,
|
||||
res,
|
||||
invitation,
|
||||
supEmail,
|
||||
additionalFields,
|
||||
dbSchema,
|
||||
}) {
|
||||
const tableSchema = dbSchema?.tables.find(
|
||||
(tb) => tb?.tableName === "users"
|
||||
);
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
try {
|
||||
////////////////////////////////////////////////
|
||||
@ -109,6 +72,7 @@ async function handleSocialDb({
|
||||
user: existingSocialIdUser[0],
|
||||
social_platform,
|
||||
res,
|
||||
invitation,
|
||||
database,
|
||||
additionalFields,
|
||||
});
|
||||
@ -137,9 +101,7 @@ async function handleSocialDb({
|
||||
|
||||
let existingEmailOnly = await varDatabaseDbHandler({
|
||||
database: database ? database : "datasquirel",
|
||||
queryString: `SELECT * FROM users WHERE email = ?`,
|
||||
queryValuesArray: [finalEmail],
|
||||
tableSchema,
|
||||
queryString: `SELECT * FROM users WHERE email='${finalEmail}'`,
|
||||
});
|
||||
|
||||
if (existingEmailOnly && existingEmailOnly[0]) {
|
||||
@ -165,6 +127,7 @@ async function handleSocialDb({
|
||||
user: payload,
|
||||
social_platform,
|
||||
res,
|
||||
invitation,
|
||||
database,
|
||||
additionalFields,
|
||||
});
|
||||
@ -176,10 +139,9 @@ async function handleSocialDb({
|
||||
|
||||
const socialHashedPassword = encrypt({
|
||||
data: social_id.toString(),
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
|
||||
/** @type {any} */
|
||||
const data = {
|
||||
social_login: "1",
|
||||
verification_status: supEmail ? "0" : "1",
|
||||
@ -187,11 +149,13 @@ async function handleSocialDb({
|
||||
};
|
||||
|
||||
Object.keys(payload).forEach((key) => {
|
||||
// @ts-ignore
|
||||
data[key] = payload[key];
|
||||
});
|
||||
|
||||
/** @type {any} */
|
||||
const newUser = await addDbEntry({
|
||||
dbContext: database ? "Dsql User" : undefined,
|
||||
paradigm: database ? "Full Access" : undefined,
|
||||
dbFullName: database ? database : "datasquirel",
|
||||
tableName: "users",
|
||||
duplicateColumnName: "email",
|
||||
@ -200,12 +164,16 @@ async function handleSocialDb({
|
||||
...data,
|
||||
email: finalEmail,
|
||||
},
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
tableSchema,
|
||||
});
|
||||
|
||||
if (newUser?.insertId) {
|
||||
if (!database) {
|
||||
/**
|
||||
* Add a Mariadb User for this User
|
||||
*/
|
||||
await addMariadbUser({ userId: newUser.insertId });
|
||||
}
|
||||
|
||||
const newUserQueried = await varDatabaseDbHandler({
|
||||
database: database ? database : "datasquirel",
|
||||
queryString: `SELECT * FROM users WHERE id='${newUser.insertId}'`,
|
||||
@ -234,9 +202,54 @@ async function handleSocialDb({
|
||||
email: supEmail,
|
||||
dateCode: Date.now(),
|
||||
}),
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
|
||||
handleNodemailer({
|
||||
to: supEmail,
|
||||
subject: "Verify Email Address",
|
||||
text: "Please click the link to verify your email address",
|
||||
html: fs
|
||||
.readFileSync(
|
||||
"./email/send-email-verification-link.html",
|
||||
"utf8"
|
||||
)
|
||||
.replace(/{{host}}/, process.env.DSQL_HOST || "")
|
||||
.replace(/{{token}}/, generatedToken || ""),
|
||||
}).then((mail) => {});
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
const STATIC_ROOT = process.env.DSQL_STATIC_SERVER_DIR;
|
||||
|
||||
if (!STATIC_ROOT) {
|
||||
console.log("Static File ENV not Found!");
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
if (!database || database?.match(/^datasquirel$/)) {
|
||||
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.insertId}`;
|
||||
|
||||
let newUserMediaFolderPath = path.join(
|
||||
STATIC_ROOT,
|
||||
`images/user-images/user-${newUser.insertId}`
|
||||
);
|
||||
|
||||
fs.mkdirSync(newUserSchemaFolderPath);
|
||||
fs.mkdirSync(newUserMediaFolderPath);
|
||||
|
||||
fs.writeFileSync(
|
||||
`${newUserSchemaFolderPath}/main.json`,
|
||||
JSON.stringify([]),
|
||||
"utf8"
|
||||
);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
@ -247,6 +260,7 @@ async function handleSocialDb({
|
||||
user: newUserQueried[0],
|
||||
social_platform,
|
||||
res,
|
||||
invitation,
|
||||
database,
|
||||
additionalFields,
|
||||
});
|
||||
@ -271,7 +285,7 @@ async function handleSocialDb({
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
} catch (/** @type {*} */ error) {
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log(
|
||||
"ERROR in 'handleSocialDb.js' backend function =>",
|
||||
error.message
|
||||
@ -282,18 +296,8 @@ async function handleSocialDb({
|
||||
user: null,
|
||||
error: error.message,
|
||||
};
|
||||
|
||||
// serverError({
|
||||
// component: "/functions/backend/social-login/handleSocialDb.js - main-catch-error",
|
||||
// message: error.message,
|
||||
// user: { first_name, last_name },
|
||||
// });
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
}
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
@ -318,20 +322,18 @@ async function handleSocialDb({
|
||||
* social_id: string|number,
|
||||
* }} params.user - user object
|
||||
* @param {string} params.social_platform - Whether its "google" or "facebook" or "github"
|
||||
* @param {http.ServerResponse} params.res - Https response object
|
||||
* @param {string|null} params.database - Target Database
|
||||
* @param {ServerResponse} [params.res] - Https response object
|
||||
* @param {any} [params.invitation] - A query object if user was invited
|
||||
* @param {string} [params.database] - Target Database
|
||||
* @param {object} [params.additionalFields] - Additional fields to be added to the user payload
|
||||
*
|
||||
* @returns {Promise<{
|
||||
* success: boolean,
|
||||
* user: { id: number, first_name: string, last_name: string } | null
|
||||
* msg?: string
|
||||
* }>}
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
async function loginSocialUser({
|
||||
user,
|
||||
social_platform,
|
||||
res,
|
||||
invitation,
|
||||
database,
|
||||
additionalFields,
|
||||
}) {
|
||||
@ -340,19 +342,18 @@ async function loginSocialUser({
|
||||
queryString: `SELECT * FROM users WHERE email='${user.email}' AND social_id='${user.social_id}' AND social_platform='${social_platform}'`,
|
||||
});
|
||||
|
||||
if (!foundUser?.[0])
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
};
|
||||
|
||||
let csrfKey =
|
||||
Math.random().toString(36).substring(2) +
|
||||
"-" +
|
||||
Math.random().toString(36).substring(2);
|
||||
|
||||
if (!foundUser?.[0]) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
msg: "User Not Found",
|
||||
};
|
||||
}
|
||||
|
||||
/** @type {any} */
|
||||
let userPayload = {
|
||||
id: foundUser[0].id,
|
||||
type: foundUser[0].type || "",
|
||||
@ -374,21 +375,18 @@ async function loginSocialUser({
|
||||
|
||||
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
||||
Object.keys(additionalFields).forEach((key) => {
|
||||
// @ts-ignore
|
||||
userPayload[key] = foundUser[0][key];
|
||||
});
|
||||
}
|
||||
|
||||
let encryptedPayload = encrypt({
|
||||
data: JSON.stringify(userPayload),
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
let encryptedPayload = encrypt({ data: JSON.stringify(userPayload) });
|
||||
|
||||
const { keyCookieName, csrfCookieName } = getAuthCookieNames();
|
||||
|
||||
if (res?.setHeader) {
|
||||
res.setHeader("Set-Cookie", [
|
||||
`datasquirelAuthKey=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
|
||||
`csrf=${csrfKey};samesite=strict;path=/;HttpOnly=true`,
|
||||
`${keyCookieName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
|
||||
`${csrfCookieName}=${csrfKey};samesite=strict;path=/;HttpOnly=true`,
|
||||
]);
|
||||
}
|
||||
|
||||
@ -396,10 +394,19 @@ async function loginSocialUser({
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
if (invitation && (!database || database?.match(/^datasquirel$/))) {
|
||||
addAdminUserOnLogin({
|
||||
query: invitation,
|
||||
user: userPayload,
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: userPayload,
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = handleSocialDb;
|
117
package-shared/functions/api/users/api-create-user.js
Normal file
117
package-shared/functions/api/users/api-create-user.js
Normal file
@ -0,0 +1,117 @@
|
||||
// @ts-check
|
||||
|
||||
const addUsersTableToDb = require("../../backend/addUsersTableToDb");
|
||||
const addDbEntry = require("../../backend/db/addDbEntry");
|
||||
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
||||
const hashPassword = require("../../dsql/hashPassword");
|
||||
|
||||
/** @type {import("../../../types").APICreateUserFunction} */
|
||||
module.exports = async function apiCreateUser({
|
||||
encryptionKey,
|
||||
payload,
|
||||
database,
|
||||
userId,
|
||||
}) {
|
||||
const dbFullName = database;
|
||||
|
||||
const hashedPassword = hashPassword({
|
||||
encryptionKey: encryptionKey,
|
||||
password: String(payload.password),
|
||||
});
|
||||
|
||||
payload.password = hashedPassword;
|
||||
|
||||
let fields = await varDatabaseDbHandler({
|
||||
queryString: `SHOW COLUMNS FROM users`,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
if (!fields) {
|
||||
const newTable = await addUsersTableToDb({
|
||||
userId: Number(userId),
|
||||
database: database,
|
||||
});
|
||||
|
||||
fields = await varDatabaseDbHandler({
|
||||
queryString: `SHOW COLUMNS FROM users`,
|
||||
database: dbFullName,
|
||||
});
|
||||
}
|
||||
|
||||
if (!fields) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Could not create users table",
|
||||
};
|
||||
}
|
||||
|
||||
const fieldsTitles = fields.map(
|
||||
(/** @type {any} */ fieldObject) => fieldObject.Field
|
||||
);
|
||||
|
||||
let invalidField = null;
|
||||
|
||||
for (let i = 0; i < Object.keys(payload).length; i++) {
|
||||
const key = Object.keys(payload)[i];
|
||||
if (!fieldsTitles.includes(key)) {
|
||||
invalidField = key;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (invalidField) {
|
||||
return {
|
||||
success: false,
|
||||
msg: `${invalidField} is not a valid field!`,
|
||||
};
|
||||
}
|
||||
|
||||
const existingUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM users WHERE email = ?${
|
||||
payload.username ? " OR username = ?" : ""
|
||||
}`,
|
||||
queryValuesArray: payload.username
|
||||
? [payload.email, payload.username]
|
||||
: [payload.email],
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
if (existingUser?.[0]) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "User Already Exists",
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
|
||||
const addUser = await addDbEntry({
|
||||
dbContext: "Dsql User",
|
||||
paradigm: "Full Access",
|
||||
dbFullName: dbFullName,
|
||||
tableName: "users",
|
||||
data: {
|
||||
...payload,
|
||||
image: "/images/user-preset.png",
|
||||
image_thumbnail: "/images/user-preset-thumbnail.png",
|
||||
},
|
||||
});
|
||||
|
||||
if (addUser?.insertId) {
|
||||
const newlyAddedUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT id,first_name,last_name,email,username,phone,image,image_thumbnail,city,state,country,zip_code,address,verification_status,more_user_data FROM users WHERE id='${addUser.insertId}'`,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: newlyAddedUser[0],
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Could not create user",
|
||||
sqlResult: addUser,
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
};
|
26
package-shared/functions/api/users/api-get-user.js
Normal file
26
package-shared/functions/api/users/api-get-user.js
Normal file
@ -0,0 +1,26 @@
|
||||
// @ts-check
|
||||
|
||||
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
||||
|
||||
/** @type {import("../../../types").APIGetUserFunction} */
|
||||
module.exports = async function apiGetUser({ fields, dbFullName, userId }) {
|
||||
const query = `SELECT ${fields.join(",")} FROM users WHERE id=?`;
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: query,
|
||||
queryValuesArray: [userId],
|
||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||
});
|
||||
|
||||
if (!foundUser || !foundUser[0]) {
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: foundUser[0],
|
||||
};
|
||||
};
|
159
package-shared/functions/api/users/api-login.js
Normal file
159
package-shared/functions/api/users/api-login.js
Normal file
@ -0,0 +1,159 @@
|
||||
// @ts-check
|
||||
|
||||
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
||||
const hashPassword = require("../../dsql/hashPassword");
|
||||
|
||||
/** @type {import("../../../types").APILoginFunction} */
|
||||
module.exports = async function apiLoginUser({
|
||||
encryptionKey,
|
||||
email,
|
||||
username,
|
||||
password,
|
||||
database,
|
||||
additionalFields,
|
||||
email_login,
|
||||
email_login_code,
|
||||
email_login_field,
|
||||
token,
|
||||
skipPassword,
|
||||
social,
|
||||
}) {
|
||||
const dbFullName = database;
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
*
|
||||
* @description Check input validity
|
||||
*/
|
||||
if (
|
||||
email?.match(/ /) ||
|
||||
(username && username?.match(/ /)) ||
|
||||
(password && password?.match(/ /))
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Invalid Email/Password format",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Password hash
|
||||
*
|
||||
* @description Password hash
|
||||
*/
|
||||
let hashedPassword = password
|
||||
? hashPassword({
|
||||
encryptionKey: encryptionKey,
|
||||
password: password,
|
||||
})
|
||||
: null;
|
||||
|
||||
let isSocialValidated = false;
|
||||
let loginFailureReason = null;
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM users WHERE email = ? OR username = ?`,
|
||||
queryValuesArray: [email, username],
|
||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||
});
|
||||
|
||||
if ((!foundUser || !foundUser[0]) && !social)
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "No user found",
|
||||
};
|
||||
|
||||
let isPasswordCorrect = false;
|
||||
|
||||
if (foundUser?.[0] && !email_login && skipPassword) {
|
||||
isPasswordCorrect = true;
|
||||
} else if (foundUser?.[0] && !email_login) {
|
||||
isPasswordCorrect = hashedPassword === foundUser[0].password;
|
||||
} else if (
|
||||
foundUser &&
|
||||
foundUser[0] &&
|
||||
email_login &&
|
||||
email_login_code &&
|
||||
email_login_field
|
||||
) {
|
||||
/** @type {string} */
|
||||
const tempCode = foundUser[0][email_login_field];
|
||||
|
||||
if (!tempCode) throw new Error("No code Found!");
|
||||
|
||||
const tempCodeArray = tempCode.split("-");
|
||||
const [code, codeDate] = tempCodeArray;
|
||||
const millisecond15mins = 1000 * 60 * 15;
|
||||
|
||||
if (Date.now() - Number(codeDate) > millisecond15mins) {
|
||||
throw new Error("Code Expired");
|
||||
}
|
||||
isPasswordCorrect = code === email_login_code;
|
||||
}
|
||||
|
||||
let socialUserValid = false;
|
||||
|
||||
if (!isPasswordCorrect && !socialUserValid) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Wrong password, no social login validity",
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
|
||||
if (isPasswordCorrect && email_login) {
|
||||
const resetTempCode = await varDatabaseDbHandler({
|
||||
queryString: `UPDATE users SET ${email_login_field} = ? WHERE email = ? OR username = ?`,
|
||||
queryValuesArray: ["", email, username],
|
||||
database: dbFullName.replace(/[^a-z0-9_]/g, ""),
|
||||
});
|
||||
}
|
||||
|
||||
let csrfKey =
|
||||
Math.random().toString(36).substring(2) +
|
||||
"-" +
|
||||
Math.random().toString(36).substring(2);
|
||||
|
||||
/** @type {import("../../../types").DATASQUIREL_LoggedInUser} */
|
||||
let userPayload = {
|
||||
id: foundUser[0].id,
|
||||
first_name: foundUser[0].first_name,
|
||||
last_name: foundUser[0].last_name,
|
||||
username: foundUser[0].username,
|
||||
email: foundUser[0].email,
|
||||
phone: foundUser[0].phone,
|
||||
social_id: foundUser[0].social_id,
|
||||
image: foundUser[0].image,
|
||||
image_thumbnail: foundUser[0].image_thumbnail,
|
||||
verification_status: foundUser[0].verification_status,
|
||||
social_login: foundUser[0].social_login,
|
||||
social_platform: foundUser[0].social_platform,
|
||||
csrf_k: csrfKey,
|
||||
more_data: foundUser[0].more_user_data,
|
||||
logged_in_status: true,
|
||||
date: Date.now(),
|
||||
};
|
||||
|
||||
const resposeObject = {
|
||||
success: true,
|
||||
msg: "Login Successful",
|
||||
payload:
|
||||
/** @type {import("../../../types").DATASQUIREL_LoggedInUser} */ (
|
||||
userPayload
|
||||
),
|
||||
userId: foundUser[0].id,
|
||||
};
|
||||
|
||||
if (
|
||||
additionalFields &&
|
||||
Array.isArray(additionalFields) &&
|
||||
additionalFields.length > 0
|
||||
) {
|
||||
additionalFields.forEach((key) => {
|
||||
userPayload[key] = foundUser[0][key];
|
||||
});
|
||||
}
|
||||
|
||||
return resposeObject;
|
||||
};
|
92
package-shared/functions/api/users/api-reauth-user.js
Normal file
92
package-shared/functions/api/users/api-reauth-user.js
Normal file
@ -0,0 +1,92 @@
|
||||
// @ts-check
|
||||
|
||||
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
||||
const nodemailer = require("nodemailer");
|
||||
|
||||
/**
|
||||
* # Re-authenticate API user
|
||||
* @param {object} param
|
||||
* @param {Object<string, any>} param.existingUser
|
||||
* @param {string} param.database
|
||||
* @param {string | number} [param.userId]
|
||||
* @param {string[]} [param.additionalFields]
|
||||
*
|
||||
* @returns {Promise<import("../../../types").ApiReauthUserReturn>}
|
||||
*/
|
||||
module.exports = async function apiReauthUser({
|
||||
existingUser,
|
||||
database,
|
||||
userId,
|
||||
additionalFields,
|
||||
}) {
|
||||
let foundUser =
|
||||
existingUser?.id && existingUser.id.toString().match(/./)
|
||||
? await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM users WHERE id=?`,
|
||||
queryValuesArray: [existingUser.id.toString()],
|
||||
database,
|
||||
})
|
||||
: null;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!foundUser || !foundUser[0])
|
||||
return {
|
||||
success: false,
|
||||
payload: null,
|
||||
msg: "No user found",
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let csrfKey =
|
||||
Math.random().toString(36).substring(2) +
|
||||
"-" +
|
||||
Math.random().toString(36).substring(2);
|
||||
|
||||
/** @type {Object<string, string | number | boolean>} */
|
||||
let userPayload = {
|
||||
id: foundUser[0].id,
|
||||
first_name: foundUser[0].first_name,
|
||||
last_name: foundUser[0].last_name,
|
||||
username: foundUser[0].username,
|
||||
email: foundUser[0].email,
|
||||
phone: foundUser[0].phone,
|
||||
social_id: foundUser[0].social_id,
|
||||
image: foundUser[0].image,
|
||||
image_thumbnail: foundUser[0].image_thumbnail,
|
||||
verification_status: foundUser[0].verification_status,
|
||||
social_login: foundUser[0].social_login,
|
||||
social_platform: foundUser[0].social_platform,
|
||||
csrf_k: csrfKey,
|
||||
more_data: foundUser[0].more_user_data,
|
||||
logged_in_status: true,
|
||||
date: Date.now(),
|
||||
};
|
||||
|
||||
if (
|
||||
additionalFields &&
|
||||
Array.isArray(additionalFields) &&
|
||||
additionalFields.length > 0
|
||||
) {
|
||||
additionalFields.forEach((key) => {
|
||||
userPayload[key] = foundUser[0][key];
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
/** ********************* Send Response */
|
||||
return {
|
||||
success: true,
|
||||
msg: "Login Successful",
|
||||
payload: userPayload,
|
||||
userId,
|
||||
};
|
||||
};
|
116
package-shared/functions/api/users/api-send-email-code.js
Normal file
116
package-shared/functions/api/users/api-send-email-code.js
Normal file
@ -0,0 +1,116 @@
|
||||
// @ts-check
|
||||
|
||||
const varDatabaseDbHandler = require("../../backend/varDatabaseDbHandler");
|
||||
const nodemailer = require("nodemailer");
|
||||
|
||||
/**
|
||||
* # Send Email Login Code
|
||||
*
|
||||
* @param {object} param
|
||||
* @param {string} param.email
|
||||
* @param {string} param.database
|
||||
* @param {string} [param.email_login_field]
|
||||
* @param {string} [param.mail_domain]
|
||||
* @param {number} [param.mail_port]
|
||||
* @param {string} [param.sender]
|
||||
* @param {string} [param.mail_username]
|
||||
* @param {string} [param.mail_password]
|
||||
* @param {string} param.html
|
||||
*
|
||||
* @returns {Promise<{success: boolean, msg?: string}>}
|
||||
*/
|
||||
module.exports = async function apiSendEmailCode({
|
||||
email,
|
||||
database,
|
||||
email_login_field,
|
||||
mail_domain,
|
||||
mail_port,
|
||||
sender,
|
||||
mail_username,
|
||||
mail_password,
|
||||
html,
|
||||
}) {
|
||||
if (email?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Invalid Email/Password format",
|
||||
};
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM users WHERE email = ?`,
|
||||
queryValuesArray: [email],
|
||||
database,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!foundUser || !foundUser[0]) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "No user found",
|
||||
};
|
||||
}
|
||||
|
||||
function generateCode() {
|
||||
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
let code = "";
|
||||
for (let i = 0; i < 8; i++) {
|
||||
code += chars[Math.floor(Math.random() * chars.length)];
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
if (foundUser && foundUser[0] && email_login_field) {
|
||||
const tempCode = generateCode();
|
||||
|
||||
let transporter = nodemailer.createTransport({
|
||||
host: mail_domain || process.env.DSQL_MAIL_HOST,
|
||||
port: mail_port || 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: mail_username || process.env.DSQL_MAIL_EMAIL,
|
||||
pass: mail_password || process.env.DSQL_MAIL_PASSWORD,
|
||||
},
|
||||
});
|
||||
|
||||
let mailObject = {};
|
||||
|
||||
mailObject["from"] = `"Datasquirel SSO" <${
|
||||
sender || "support@datasquirel.com"
|
||||
}>`;
|
||||
mailObject["sender"] = sender || "support@datasquirel.com";
|
||||
mailObject["to"] = email;
|
||||
mailObject["subject"] = "One Time Login Code";
|
||||
mailObject["html"] = html.replace(/{{code}}/, tempCode);
|
||||
|
||||
const info = await transporter.sendMail(mailObject);
|
||||
|
||||
if (!info?.accepted) throw new Error("Mail not Sent!");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let setTempCode = await varDatabaseDbHandler({
|
||||
queryString: `UPDATE users SET ${email_login_field} = ? WHERE email = ?`,
|
||||
queryValuesArray: [tempCode + `-${Date.now()}`, email],
|
||||
database: database,
|
||||
});
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return {
|
||||
success: true,
|
||||
msg: "Success",
|
||||
};
|
||||
};
|
43
package-shared/functions/api/users/api-update-user.js
Normal file
43
package-shared/functions/api/users/api-update-user.js
Normal file
@ -0,0 +1,43 @@
|
||||
// @ts-check
|
||||
|
||||
const updateDbEntry = require("../../backend/db/updateDbEntry");
|
||||
|
||||
/**
|
||||
* # Update API User Function
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {{ id: string | number } & Object<string, (string | number | null | undefined)>} params.payload
|
||||
* @param {string} params.dbFullName
|
||||
*
|
||||
* @returns {Promise<{ success: boolean, payload: any }>}
|
||||
*/
|
||||
module.exports = async function apiUpdateUser({ payload, dbFullName }) {
|
||||
const data = (() => {
|
||||
const reqBodyKeys = Object.keys(payload);
|
||||
|
||||
/** @type {any} */
|
||||
const finalData = {};
|
||||
|
||||
reqBodyKeys.forEach((key) => {
|
||||
if (key?.match(/^date_|^id$/)) return;
|
||||
finalData[key] = payload[key];
|
||||
});
|
||||
|
||||
return finalData;
|
||||
})();
|
||||
|
||||
const updateUser = await updateDbEntry({
|
||||
dbContext: "Dsql User",
|
||||
paradigm: "Full Access",
|
||||
dbFullName,
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: payload.id,
|
||||
data: data,
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: updateUser,
|
||||
};
|
||||
};
|
110
package-shared/functions/api/users/social/api-github-login.js
Normal file
110
package-shared/functions/api/users/social/api-github-login.js
Normal file
@ -0,0 +1,110 @@
|
||||
// @ts-check
|
||||
|
||||
const handleSocialDb = require("../../social-login/handleSocialDb");
|
||||
const githubLogin = require("../../social-login/githubLogin");
|
||||
const camelJoinedtoCamelSpace = require("../../../../utils/camelJoinedtoCamelSpace");
|
||||
|
||||
/**
|
||||
* # Login with Github
|
||||
* @param {object} param
|
||||
* @param {string} [param.code]
|
||||
* @param {string} [param.clientId]
|
||||
* @param {string} [param.clientSecret]
|
||||
* @param {string} [param.database]
|
||||
* @param {Object<string, any>} [param.additionalFields]
|
||||
* @param {any} [param.res]
|
||||
* @param {string} [param.email]
|
||||
* @param {string | number} [param.userId]
|
||||
*
|
||||
* @returns {Promise<import("../../../../types").APIGoogleLoginFunctionReturn>}
|
||||
*/
|
||||
module.exports = async function apiGithubLogin({
|
||||
code,
|
||||
clientId,
|
||||
clientSecret,
|
||||
database,
|
||||
additionalFields,
|
||||
res,
|
||||
email,
|
||||
userId,
|
||||
}) {
|
||||
if (!code || !clientId || !clientSecret || !database) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Missing query params",
|
||||
};
|
||||
}
|
||||
|
||||
if (
|
||||
typeof code !== "string" ||
|
||||
typeof clientId !== "string" ||
|
||||
typeof clientSecret !== "string" ||
|
||||
typeof database !== "string"
|
||||
) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Wrong Parameters",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
const gitHubUser = await githubLogin({
|
||||
code: code,
|
||||
clientId: clientId,
|
||||
clientSecret: clientSecret,
|
||||
});
|
||||
|
||||
if (!gitHubUser) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "No github user returned",
|
||||
};
|
||||
}
|
||||
|
||||
const socialId = gitHubUser.name || gitHubUser.id || gitHubUser.login;
|
||||
const targetName = gitHubUser.name || gitHubUser.login;
|
||||
const nameArray = targetName?.match(/ /)
|
||||
? targetName?.split(" ")
|
||||
: targetName?.match(/\-/)
|
||||
? targetName?.split("-")
|
||||
: [targetName];
|
||||
|
||||
const payload = {
|
||||
email: gitHubUser.email,
|
||||
first_name: camelJoinedtoCamelSpace(nameArray[0]),
|
||||
last_name: camelJoinedtoCamelSpace(nameArray[1]),
|
||||
social_id: socialId,
|
||||
social_platform: "github",
|
||||
image: gitHubUser.avatar_url,
|
||||
image_thumbnail: gitHubUser.avatar_url,
|
||||
username: "github-user-" + socialId,
|
||||
};
|
||||
|
||||
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
||||
Object.keys(additionalFields).forEach((key) => {
|
||||
// @ts-ignore
|
||||
payload[key] = additionalFields[key];
|
||||
});
|
||||
}
|
||||
|
||||
const loggedInGithubUser = await handleSocialDb({
|
||||
database,
|
||||
email: gitHubUser.email,
|
||||
payload: payload,
|
||||
social_platform: "github",
|
||||
res: res,
|
||||
social_id: socialId,
|
||||
supEmail: email,
|
||||
additionalFields,
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return { success: true, ...loggedInGithubUser, dsqlUserId: userId };
|
||||
};
|
@ -0,0 +1,88 @@
|
||||
// @ts-check
|
||||
|
||||
const { OAuth2Client } = require("google-auth-library");
|
||||
const handleSocialDb = require("../../social-login/handleSocialDb");
|
||||
|
||||
/** @type {import("../../../../types").APIGoogleLoginFunction} */
|
||||
module.exports = async function apiGoogleLogin({
|
||||
clientId,
|
||||
token,
|
||||
database,
|
||||
userId,
|
||||
additionalFields,
|
||||
res,
|
||||
}) {
|
||||
const client = new OAuth2Client(clientId);
|
||||
|
||||
const ticket = await client.verifyIdToken({
|
||||
idToken: token,
|
||||
audience: clientId,
|
||||
});
|
||||
|
||||
if (!ticket?.getPayload()?.email_verified) {
|
||||
return {
|
||||
success: false,
|
||||
user: null,
|
||||
};
|
||||
}
|
||||
|
||||
const payload = ticket.getPayload();
|
||||
|
||||
if (!payload) throw new Error("No Payload");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (!database || typeof database != "string" || database?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
user: undefined,
|
||||
msg: "Please provide a database slug(database name in lowercase with no spaces)",
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create new user folder and file
|
||||
*
|
||||
* @description Create new user folder and file
|
||||
*/
|
||||
const targetDbName = `datasquirel_user_${userId}_${database}`;
|
||||
|
||||
const { given_name, family_name, email, sub, picture, email_verified } =
|
||||
payload;
|
||||
|
||||
/** @type {Object<string, any>} */
|
||||
const payloadObject = {
|
||||
email: email,
|
||||
first_name: given_name,
|
||||
last_name: family_name,
|
||||
social_id: sub,
|
||||
social_platform: "google",
|
||||
image: picture,
|
||||
image_thumbnail: picture,
|
||||
username: `google-user-${sub}`,
|
||||
};
|
||||
|
||||
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
||||
Object.keys(additionalFields).forEach((key) => {
|
||||
payloadObject[key] = additionalFields[key];
|
||||
});
|
||||
}
|
||||
|
||||
const loggedInGoogleUser = await handleSocialDb({
|
||||
res,
|
||||
database: targetDbName,
|
||||
email: email || "",
|
||||
payload: payloadObject,
|
||||
social_platform: "google",
|
||||
social_id: sub,
|
||||
additionalFields,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
return { success: true, ...loggedInGoogleUser, dsqlUserId: userId };
|
||||
};
|
191
package-shared/functions/backend/addAdminUserOnLogin.js
Executable file
191
package-shared/functions/backend/addAdminUserOnLogin.js
Executable file
@ -0,0 +1,191 @@
|
||||
// @ts-check
|
||||
|
||||
const serverError = require("./serverError");
|
||||
const DB_HANDLER = require("../../utils/backend/global-db/DB_HANDLER");
|
||||
const addDbEntry = require("./db/addDbEntry");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Add Admin User on Login
|
||||
* ==============================================================================
|
||||
*
|
||||
* @description this function handles admin users that have been invited by another
|
||||
* admin user. This fires when the invited user has been logged in or a new account
|
||||
* has been created for the invited user
|
||||
*
|
||||
* @param {object} params - parameters object
|
||||
*
|
||||
* @param {object} params.query - query object
|
||||
* @param {number} params.query.invite - Invitation user id
|
||||
* @param {string} params.query.database_access - String containing authorized databases
|
||||
* @param {string} params.query.priviledge - String containing databases priviledges
|
||||
* @param {string} params.query.email - Inviting user email address
|
||||
*
|
||||
* @param {import("../../types").UserType} params.user - invited user object
|
||||
*
|
||||
* @returns {Promise<any>} new user auth object payload
|
||||
*/
|
||||
module.exports = async function addAdminUserOnLogin({ query, user }) {
|
||||
try {
|
||||
/**
|
||||
* Fetch user
|
||||
*
|
||||
* @description Fetch user from db
|
||||
*/ // @ts-ignore
|
||||
const { invite, database_access, priviledge, email } = query;
|
||||
|
||||
const lastInviteTimeArray = await DB_HANDLER(
|
||||
`SELECT date_created_code FROM invitations WHERE inviting_user_id=? AND invited_user_email=?`,
|
||||
[invite, email]
|
||||
);
|
||||
|
||||
// if (lastInviteTimeArray && lastInviteTimeArray[0]?.date_created_code) {
|
||||
// const timeSinceLastInvite = Date.now() - parseInt(lastInviteTimeArray[0].date_created_code);
|
||||
// if (timeSinceLastInvite > 21600000) {
|
||||
// throw new Error("Invitation expired");
|
||||
// }
|
||||
// } else if (!lastInviteTimeArray || !lastInviteTimeArray[0]) {
|
||||
// throw new Error("No Invitation Found");
|
||||
// }
|
||||
|
||||
if (!lastInviteTimeArray || !lastInviteTimeArray[0]) {
|
||||
throw new Error("No Invitation Found");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
// @ts-ignore
|
||||
const invitingUserDb = await DB_HANDLER(
|
||||
`SELECT first_name,last_name,email FROM users WHERE id=?`,
|
||||
[invite]
|
||||
);
|
||||
|
||||
if (invitingUserDb?.[0]) {
|
||||
const existingUserUser = await DB_HANDLER(
|
||||
`SELECT email FROM user_users WHERE user_id=? AND invited_user_id=? AND user_type='admin' AND email=?`,
|
||||
[invite, user.id, email]
|
||||
);
|
||||
|
||||
if (existingUserUser?.[0]) {
|
||||
console.log("User already added");
|
||||
} else {
|
||||
// const newUserUser = await DB_HANDLER(
|
||||
// `INSERT IGNORE INTO user_users
|
||||
// (user_id, invited_user_id, database_access, first_name, last_name, phone, email, username, user_type, user_priviledge)
|
||||
// VALUES
|
||||
// (?,?,?,?,?,?,?,?,?,?)
|
||||
// )`,
|
||||
// [
|
||||
// invite,
|
||||
// user.id,
|
||||
// database_access,
|
||||
// user.first_name,
|
||||
// user.last_name,
|
||||
// user.phone,
|
||||
// user.email,
|
||||
// user.username,
|
||||
// "admin",
|
||||
// priviledge,
|
||||
// ]
|
||||
// );
|
||||
addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_users",
|
||||
data: {
|
||||
user_id: invite,
|
||||
invited_user_id: user.id,
|
||||
database_access: database_access,
|
||||
first_name: user.first_name,
|
||||
last_name: user.last_name,
|
||||
phone: user.phone,
|
||||
email: user.email,
|
||||
username: user.username,
|
||||
user_type: "admin",
|
||||
user_priviledge: priviledge,
|
||||
image: user.image,
|
||||
image_thumbnail: user.image_thumbnail,
|
||||
},
|
||||
});
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
// @ts-ignore
|
||||
const dbTableData = await DB_HANDLER(
|
||||
`SELECT db_tables_data FROM invitations WHERE inviting_user_id=? AND invited_user_email=?`,
|
||||
[invite, email]
|
||||
);
|
||||
|
||||
// @ts-ignore
|
||||
const clearEntries = await DB_HANDLER(
|
||||
`DELETE FROM delegated_user_tables WHERE root_user_id=? AND delegated_user_id=?`,
|
||||
[invite, user.id]
|
||||
);
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
if (dbTableData && dbTableData[0]) {
|
||||
const dbTableEntries =
|
||||
dbTableData[0].db_tables_data.split("|");
|
||||
|
||||
for (let i = 0; i < dbTableEntries.length; i++) {
|
||||
const dbTableEntry = dbTableEntries[i];
|
||||
const dbTableEntryArray = dbTableEntry.split("-");
|
||||
const [db_slug, table_slug] = dbTableEntryArray;
|
||||
|
||||
const newEntry = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "delegated_user_tables",
|
||||
data: {
|
||||
delegated_user_id: user.id,
|
||||
root_user_id: invite,
|
||||
database: db_slug,
|
||||
table: table_slug,
|
||||
priviledge: priviledge,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
}
|
||||
|
||||
// @ts-ignore
|
||||
const inviteAccepted = await DB_HANDLER(
|
||||
`UPDATE invitations SET invitation_status='Accepted' WHERE inviting_user_id=? AND invited_user_email=?`,
|
||||
[invite, email]
|
||||
);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
|
||||
serverError({
|
||||
component: "addAdminUserOnLogin",
|
||||
message: error.message,
|
||||
user: user,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
@ -3,8 +3,8 @@
|
||||
const generator = require("generate-password");
|
||||
const DB_HANDLER = require("../../utils/backend/global-db/DB_HANDLER");
|
||||
const NO_DB_HANDLER = require("../../utils/backend/global-db/NO_DB_HANDLER");
|
||||
const encrypt = require("./encrypt");
|
||||
const addDbEntry = require("./db/addDbEntry");
|
||||
const encrypt = require("../dsql/encrypt");
|
||||
|
||||
/**
|
||||
* # Add Mariadb User
|
||||
@ -28,7 +28,7 @@ module.exports = async function addMariadbUser({ userId }) {
|
||||
uppercase: true,
|
||||
exclude: "*#.'`\"",
|
||||
});
|
||||
const encryptedPassword = encrypt(password);
|
||||
const encryptedPassword = encrypt({ data: password });
|
||||
|
||||
await NO_DB_HANDLER(
|
||||
`CREATE USER IF NOT EXISTS '${username}'@'127.0.0.1' IDENTIFIED BY '${password}' REQUIRE SSL`
|
||||
|
95
package-shared/functions/backend/addUsersTableToDb.js
Executable file
95
package-shared/functions/backend/addUsersTableToDb.js
Executable file
@ -0,0 +1,95 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const { execSync } = require("child_process");
|
||||
const serverError = require("./serverError");
|
||||
const DB_HANDLER = require("../../utils/backend/global-db/DB_HANDLER");
|
||||
const { default: grabUserSchemaData } = require("./grabUserSchemaData");
|
||||
const { default: setUserSchemaData } = require("./setUserSchemaData");
|
||||
const addDbEntry = require("./db/addDbEntry");
|
||||
const createDbFromSchema = require("../../shell/createDbFromSchema");
|
||||
|
||||
/**
|
||||
* # Add User Table to Database
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {number} params.userId - user id
|
||||
* @param {string} params.database
|
||||
*
|
||||
* @returns {Promise<any>} new user auth object payload
|
||||
*/
|
||||
module.exports = async function addUsersTableToDb({ userId, database }) {
|
||||
/**
|
||||
* Initialize
|
||||
*
|
||||
* @description Initialize
|
||||
*/
|
||||
const dbFullName = `datasquirel_user_${userId}_${database}`;
|
||||
/** @type {import("../../types").DSQL_TableSchemaType} */
|
||||
const userPreset = require("../../data/presets/users.json");
|
||||
|
||||
try {
|
||||
/**
|
||||
* Fetch user
|
||||
*
|
||||
* @description Fetch user from db
|
||||
*/
|
||||
const userSchemaData = grabUserSchemaData({ userId });
|
||||
if (!userSchemaData) throw new Error("User schema data not found!");
|
||||
|
||||
let targetDatabase = userSchemaData.filter(
|
||||
(db) => db.dbSlug === database
|
||||
)[0];
|
||||
|
||||
let existingTableIndex;
|
||||
// @ts-ignore
|
||||
let existingTable = targetDatabase.tables.filter((table, index) => {
|
||||
if (table.tableName === "users") {
|
||||
existingTableIndex = index;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
||||
if (existingTable && existingTable[0] && existingTableIndex) {
|
||||
targetDatabase.tables[existingTableIndex] = userPreset;
|
||||
} else {
|
||||
targetDatabase.tables.push(userPreset);
|
||||
}
|
||||
|
||||
setUserSchemaData({ schemaData: userSchemaData, userId });
|
||||
|
||||
const targetDb = await DB_HANDLER(
|
||||
`SELECT id FROM user_databases WHERE user_id=? AND db_slug=?`,
|
||||
[userId, database]
|
||||
);
|
||||
|
||||
if (targetDb && targetDb[0]) {
|
||||
const newTableEntry = await addDbEntry({
|
||||
dbFullName: "datasquirel",
|
||||
tableName: "user_database_tables",
|
||||
data: {
|
||||
user_id: userId,
|
||||
db_id: targetDb[0].id,
|
||||
db_slug: database,
|
||||
table_name: "Users",
|
||||
table_slug: "users",
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
const dbShellUpdate = await createDbFromSchema({
|
||||
userId,
|
||||
targetDatabase: dbFullName,
|
||||
});
|
||||
|
||||
return `Done!`;
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "addUsersTableToDb",
|
||||
message: error.message,
|
||||
user: { id: userId },
|
||||
});
|
||||
return error.message;
|
||||
}
|
||||
};
|
@ -1,7 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
const decrypt = require("./decrypt");
|
||||
const decrypt = require("../dsql/decrypt");
|
||||
|
||||
/** @type {import("../../types").CheckApiCredentialsFn} */
|
||||
const grabApiCred = ({ key, database, table, user_id }) => {
|
||||
@ -16,7 +16,7 @@ const grabApiCred = ({ key, database, table, user_id }) => {
|
||||
"process.env.DSQL_API_KEYS_PATH variable not found"
|
||||
);
|
||||
|
||||
const ApiJSON = decrypt(key);
|
||||
const ApiJSON = decrypt({ encryptedString: key });
|
||||
/** @type {import("../../types").ApiKeyObject} */
|
||||
const ApiObject = JSON.parse(ApiJSON || "");
|
||||
const isApiKeyValid = fs.existsSync(
|
||||
|
@ -0,0 +1,13 @@
|
||||
module.exports = function getAuthCookieNames() {
|
||||
const cookiesPrefix = process.env.DSQL_COOKIES_PREFIX || "dsql_";
|
||||
const cookiesKeyName = process.env.DSQL_COOKIES_KEY_NAME || "key";
|
||||
const cookiesCSRFName = process.env.DSQL_COOKIES_CSRF_NAME || "csrf";
|
||||
|
||||
const keyCookieName = cookiesPrefix + cookiesKeyName;
|
||||
const csrfCookieName = cookiesPrefix + cookiesCSRFName;
|
||||
|
||||
return {
|
||||
keyCookieName,
|
||||
csrfCookieName,
|
||||
};
|
||||
};
|
@ -1,9 +1,5 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Imports: Handle imports
|
||||
*/
|
||||
const encrypt = require("../encrypt");
|
||||
const sanitizeHtml = require("sanitize-html");
|
||||
const sanitizeHtmlOptions = require("../html/sanitizeHtmlOptions");
|
||||
const updateDb = require("./updateDbEntry");
|
||||
@ -11,6 +7,7 @@ const updateDbEntry = require("./updateDbEntry");
|
||||
const _ = require("lodash");
|
||||
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
||||
const DSQL_USER_DB_HANDLER = require("../../../utils/backend/global-db/DSQL_USER_DB_HANDLER");
|
||||
const encrypt = require("../../dsql/encrypt");
|
||||
|
||||
/**
|
||||
* Add a db Entry Function
|
||||
@ -146,7 +143,11 @@ async function addDbEntry({
|
||||
continue;
|
||||
|
||||
if (targetFieldSchema?.encrypted) {
|
||||
value = encrypt(value, encryptionKey, encryptionSalt);
|
||||
value = encrypt({
|
||||
data: value,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
console.log("DSQL: Encrypted value =>", value);
|
||||
}
|
||||
|
||||
|
@ -1,30 +0,0 @@
|
||||
export = runQuery;
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/**
|
||||
* Run DSQL users queries
|
||||
* ==============================================================================
|
||||
* @param {object} params - An object containing the function parameters.
|
||||
* @param {string} params.dbFullName - Database full name. Eg. "datasquire_user_2_test"
|
||||
* @param {string | any} params.query - Query string or object
|
||||
* @param {boolean} [params.readOnly] - Is this operation read only?
|
||||
* @param {boolean} [params.local] - Is this operation read only?
|
||||
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
|
||||
* @param {string[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
|
||||
* @param {string} [params.tableName] - Table Name
|
||||
*
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
declare function runQuery({ dbFullName, query, readOnly, dbSchema, queryValuesArray, tableName, local, }: {
|
||||
dbFullName: string;
|
||||
query: string | any;
|
||||
readOnly?: boolean;
|
||||
local?: boolean;
|
||||
dbSchema?: import("../../../types").DSQL_DatabaseSchemaType;
|
||||
queryValuesArray?: string[];
|
||||
tableName?: string;
|
||||
}): Promise<any>;
|
@ -38,7 +38,7 @@ const trimSql = require("../../../utils/trim-sql");
|
||||
* @param {boolean} [params.readOnly] - Is this operation read only?
|
||||
* @param {boolean} [params.local] - Is this operation read only?
|
||||
* @param {import("../../../types").DSQL_DatabaseSchemaType} [params.dbSchema] - Database schema
|
||||
* @param {string[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
|
||||
* @param {(string | number)[]} [params.queryValuesArray] - An optional array of query values if "?" is used in the query string
|
||||
* @param {string} [params.tableName] - Table Name
|
||||
*
|
||||
* @return {Promise<any>}
|
||||
@ -120,14 +120,14 @@ async function runQuery({
|
||||
} else if (readOnly) {
|
||||
result = await varReadOnlyDatabaseDbHandler({
|
||||
queryString: formattedQuery,
|
||||
queryValuesArray,
|
||||
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
|
||||
database: dbFullName,
|
||||
tableSchema,
|
||||
});
|
||||
} else {
|
||||
result = await fullAccessDbHandler({
|
||||
queryString: formattedQuery,
|
||||
queryValuesArray,
|
||||
queryValuesArray: queryValuesArray?.map((vl) => String(vl)),
|
||||
database: dbFullName,
|
||||
tableSchema,
|
||||
});
|
||||
|
@ -3,11 +3,11 @@
|
||||
/**
|
||||
* Imports: Handle imports
|
||||
*/
|
||||
const encrypt = require("../encrypt");
|
||||
const sanitizeHtml = require("sanitize-html");
|
||||
const sanitizeHtmlOptions = require("../html/sanitizeHtmlOptions");
|
||||
const DB_HANDLER = require("../../../utils/backend/global-db/DB_HANDLER");
|
||||
const DSQL_USER_DB_HANDLER = require("../../../utils/backend/global-db/DSQL_USER_DB_HANDLER");
|
||||
const encrypt = require("../../dsql/encrypt");
|
||||
|
||||
/**
|
||||
* Update DB Function
|
||||
@ -94,7 +94,11 @@ async function updateDbEntry({
|
||||
}
|
||||
|
||||
if (targetFieldSchema?.encrypted) {
|
||||
value = encrypt(value, encryptionKey, encryptionSalt);
|
||||
value = encrypt({
|
||||
data: value,
|
||||
encryptionKey,
|
||||
encryptionSalt,
|
||||
});
|
||||
}
|
||||
|
||||
if (typeof value === "object") {
|
||||
|
@ -1,6 +0,0 @@
|
||||
export = decrypt;
|
||||
/**
|
||||
* @param {string} encryptedString
|
||||
* @returns {string | null}
|
||||
*/
|
||||
declare function decrypt(encryptedString: string): string | null;
|
@ -1,29 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const { scryptSync, createDecipheriv } = require("crypto");
|
||||
const { Buffer } = require("buffer");
|
||||
|
||||
/**
|
||||
* @param {string} encryptedString
|
||||
* @returns {string | null}
|
||||
*/
|
||||
const decrypt = (encryptedString) => {
|
||||
const algorithm = "aes-192-cbc";
|
||||
const password = process.env.DSQL_ENCRYPTION_PASSWORD || "";
|
||||
const salt = process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
|
||||
let key = scryptSync(password, salt, 24);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
const decipher = createDecipheriv(algorithm, key, iv);
|
||||
|
||||
try {
|
||||
let decrypted = decipher.update(encryptedString, "hex", "utf8");
|
||||
decrypted += decipher.final("utf8");
|
||||
return decrypted;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = decrypt;
|
@ -1,9 +0,0 @@
|
||||
export = encrypt;
|
||||
/**
|
||||
* @async
|
||||
* @param {string} data
|
||||
* @param {string} [encryptionKey]
|
||||
* @param {string} [encryptionSalt]
|
||||
* @returns {string | null}
|
||||
*/
|
||||
declare function encrypt(data: string, encryptionKey?: string, encryptionSalt?: string): string | null;
|
@ -1,43 +0,0 @@
|
||||
// @ts-check
|
||||
|
||||
const { scryptSync, createCipheriv } = require("crypto");
|
||||
const { Buffer } = require("buffer");
|
||||
const serverError = require("./serverError");
|
||||
|
||||
/**
|
||||
* @async
|
||||
* @param {string} data
|
||||
* @param {string} [encryptionKey]
|
||||
* @param {string} [encryptionSalt]
|
||||
* @returns {string | null}
|
||||
*/
|
||||
const encrypt = (data, encryptionKey, encryptionSalt) => {
|
||||
const algorithm = "aes-192-cbc";
|
||||
const password = encryptionKey
|
||||
? encryptionKey
|
||||
: process.env.DSQL_ENCRYPTION_PASSWORD || "";
|
||||
|
||||
/** ********************* Generate key */
|
||||
const salt = encryptionSalt
|
||||
? encryptionSalt
|
||||
: process.env.DSQL_ENCRYPTION_SALT || "";
|
||||
let key = scryptSync(password, salt, 24);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
const cipher = createCipheriv(algorithm, key, iv);
|
||||
|
||||
/** ********************* Encrypt data */
|
||||
try {
|
||||
let encrypted = cipher.update(data, "utf8", "hex");
|
||||
encrypted += cipher.final("hex");
|
||||
return encrypted;
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "encrypt",
|
||||
message: error.message,
|
||||
});
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = encrypt;
|
46
package-shared/functions/backend/grabUserSchemaData.js
Executable file
46
package-shared/functions/backend/grabUserSchemaData.js
Executable file
@ -0,0 +1,46 @@
|
||||
// @ts-check
|
||||
|
||||
const serverError = require("./serverError");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* @param {Object} params
|
||||
* @param {string | number} params.userId
|
||||
* @returns {import("../../types").DSQL_DatabaseSchemaType[] | null}
|
||||
*/
|
||||
export default function grabUserSchemaData({ userId }) {
|
||||
try {
|
||||
const userSchemaFilePath = path.resolve(
|
||||
process.cwd(),
|
||||
`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${userId}/main.json`
|
||||
);
|
||||
const userSchemaData = JSON.parse(
|
||||
fs.readFileSync(userSchemaFilePath, "utf-8")
|
||||
);
|
||||
|
||||
return userSchemaData;
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "grabUserSchemaData",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
129
package-shared/functions/backend/handleNodemailer.js
Executable file
129
package-shared/functions/backend/handleNodemailer.js
Executable file
@ -0,0 +1,129 @@
|
||||
// @ts-check
|
||||
|
||||
/**
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
|
||||
const fs = require("fs");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
const nodemailer = require("nodemailer");
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let transporter = nodemailer.createTransport({
|
||||
host: process.env.DSQL_MAIL_HOST,
|
||||
port: 465,
|
||||
secure: true,
|
||||
auth: {
|
||||
user: process.env.DSQL_MAIL_EMAIL,
|
||||
pass: process.env.DSQL_MAIL_PASSWORD,
|
||||
},
|
||||
});
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* # Handle mails
|
||||
* @param {object} mailObject - Mail Object with params
|
||||
* @param {string} [mailObject.to] - who is recieving this email? Comma separated for multiple recipients
|
||||
* @param {string} [mailObject.subject] - Mail Subject
|
||||
* @param {string} [mailObject.text] - Mail text
|
||||
* @param {string} [mailObject.html] - Mail HTML
|
||||
* @param {string | null} [mailObject.alias] - Sender alias: "support" or null
|
||||
*
|
||||
* @returns {Promise<any>} mail object
|
||||
*/
|
||||
module.exports = async function handleNodemailer({
|
||||
to,
|
||||
subject,
|
||||
text,
|
||||
html,
|
||||
alias,
|
||||
}) {
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
if (
|
||||
!process.env.DSQL_MAIL_HOST ||
|
||||
!process.env.DSQL_MAIL_EMAIL ||
|
||||
!process.env.DSQL_MAIL_PASSWORD
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const sender = (() => {
|
||||
if (alias?.match(/support/i)) return process.env.DSQL_MAIL_EMAIL;
|
||||
return process.env.DSQL_MAIL_EMAIL;
|
||||
})();
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
let sentMessage;
|
||||
|
||||
if (!fs.existsSync("./email/index.html")) {
|
||||
return;
|
||||
}
|
||||
|
||||
let mailRoot = fs.readFileSync("./email/index.html", "utf8");
|
||||
let finalHtml = mailRoot
|
||||
.replace(/{{email_body}}/, html ? html : "")
|
||||
.replace(/{{issue_date}}/, Date().substring(0, 24));
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
try {
|
||||
let mailObject = {};
|
||||
|
||||
mailObject["from"] = `"Datasquirel" <${sender}>`;
|
||||
mailObject["sender"] = sender;
|
||||
if (alias) mailObject["replyTo "] = sender;
|
||||
// mailObject["priority"] = "high";
|
||||
mailObject["to"] = to;
|
||||
mailObject["subject"] = subject;
|
||||
mailObject["text"] = text;
|
||||
mailObject["html"] = finalHtml;
|
||||
|
||||
// send mail with defined transport object
|
||||
let info = await transporter.sendMail(mailObject);
|
||||
|
||||
sentMessage = info;
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
} catch (/** @type {any} */ error) {
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
console.log("ERROR in handleNodemailer Function =>", error.message);
|
||||
// serverError({
|
||||
// component: "handleNodemailer",
|
||||
// message: error.message,
|
||||
// user: { email: to },
|
||||
// });
|
||||
}
|
||||
|
||||
return sentMessage;
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
@ -4,7 +4,9 @@
|
||||
* Imports
|
||||
* ==============================================================================
|
||||
*/
|
||||
const grabHostNames = require("../../../../package-shared/utils/grab-host-names");
|
||||
const https = require("https");
|
||||
const http = require("http");
|
||||
const { URL } = require("url");
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
@ -17,19 +19,29 @@ const grabHostNames = require("../../../../package-shared/utils/grab-host-names"
|
||||
* Main Function
|
||||
* ==============================================================================
|
||||
* @param {{
|
||||
* scheme?: string,
|
||||
* url?: string,
|
||||
* method: string,
|
||||
* hostname: string,
|
||||
* method?: string,
|
||||
* hostname?: string,
|
||||
* path?: string,
|
||||
* href?: string,
|
||||
* port?: number | string,
|
||||
* headers?: object,
|
||||
* body?: object,
|
||||
* }} params - params
|
||||
*/
|
||||
function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
||||
module.exports = function httpsRequest({
|
||||
url,
|
||||
method,
|
||||
hostname,
|
||||
path,
|
||||
headers,
|
||||
body,
|
||||
port,
|
||||
scheme,
|
||||
}) {
|
||||
const reqPayloadString = body ? JSON.stringify(body) : null;
|
||||
|
||||
const { host, port, scheme } = grabHostNames();
|
||||
const PARSED_URL = url ? new URL(url) : null;
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
@ -37,21 +49,29 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
||||
|
||||
/** @type {any} */
|
||||
let requestOptions = {
|
||||
method: method,
|
||||
hostname: host,
|
||||
port,
|
||||
method: method || "GET",
|
||||
hostname: PARSED_URL ? PARSED_URL.hostname : hostname,
|
||||
port: scheme?.match(/https/i)
|
||||
? 443
|
||||
: PARSED_URL
|
||||
? PARSED_URL.protocol?.match(/https/i)
|
||||
? 443
|
||||
: PARSED_URL.port
|
||||
: port
|
||||
? Number(port)
|
||||
: 80,
|
||||
headers: {},
|
||||
};
|
||||
|
||||
if (path) requestOptions.path = path;
|
||||
if (href) requestOptions.href = href;
|
||||
// if (href) requestOptions.href = href;
|
||||
|
||||
if (headers) requestOptions.headers = headers;
|
||||
if (body) {
|
||||
requestOptions.headers["Content-Type"] = "application/json";
|
||||
requestOptions.headers["Content-Length"] = Buffer.from(
|
||||
reqPayloadString || ""
|
||||
).length;
|
||||
requestOptions.headers["Content-Length"] = reqPayloadString
|
||||
? Buffer.from(reqPayloadString).length
|
||||
: undefined;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////
|
||||
@ -59,10 +79,15 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
||||
////////////////////////////////////////////////
|
||||
|
||||
return new Promise((res, rej) => {
|
||||
const httpsRequest = scheme.request(
|
||||
const httpsRequest = (
|
||||
scheme?.match(/https/i)
|
||||
? https
|
||||
: PARSED_URL?.protocol?.match(/https/i)
|
||||
? https
|
||||
: http
|
||||
).request(
|
||||
/* ====== Request Options object ====== */
|
||||
// @ts-ignore
|
||||
url ? url : requestOptions,
|
||||
requestOptions,
|
||||
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
@ -84,6 +109,11 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
||||
|
||||
response.on("error", (error) => {
|
||||
console.log("HTTP response error =>", error.message);
|
||||
rej(`HTTP response error =>, ${error.message}`);
|
||||
});
|
||||
|
||||
response.on("close", () => {
|
||||
console.log("HTTP(S) Response Closed Successfully");
|
||||
});
|
||||
}
|
||||
);
|
||||
@ -91,7 +121,8 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
||||
if (body) httpsRequest.write(reqPayloadString);
|
||||
|
||||
httpsRequest.on("error", (error) => {
|
||||
console.log("HTTPS request ERROR =>", error);
|
||||
console.log("HTTPS request ERROR =>", error.message);
|
||||
rej(`HTTP request error =>, ${error.message}`);
|
||||
});
|
||||
|
||||
httpsRequest.end();
|
||||
@ -100,7 +131,7 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
||||
////////////////////////////////////////////////
|
||||
////////////////////////////////////////////////
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
@ -108,5 +139,3 @@ function httpsRequest({ url, method, hostname, path, href, headers, body }) {
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
module.exports = httpsRequest;
|
@ -1,6 +1,6 @@
|
||||
// @ts-check
|
||||
|
||||
const decrypt = require("./decrypt");
|
||||
const decrypt = require("../dsql/decrypt");
|
||||
const defaultFieldsRegexp = require("./defaultFieldsRegexp");
|
||||
|
||||
/**
|
||||
@ -55,7 +55,9 @@ module.exports = async function parseDbResults({
|
||||
|
||||
if (resultFieldSchema?.encrypted) {
|
||||
if (value?.match(/./)) {
|
||||
result[resultFieldName] = decrypt(value);
|
||||
result[resultFieldName] = decrypt({
|
||||
encryptedString: value,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +0,0 @@
|
||||
declare function _exports({ user, message, component, noMail, }: {
|
||||
user?: {
|
||||
id?: number | string;
|
||||
first_name?: string;
|
||||
last_name?: string;
|
||||
email?: string;
|
||||
} & any;
|
||||
message: string;
|
||||
component?: string;
|
||||
noMail?: boolean;
|
||||
}): Promise<void>;
|
||||
export = _exports;
|
74
package-shared/functions/backend/serverError.js
Normal file → Executable file
74
package-shared/functions/backend/serverError.js
Normal file → Executable file
@ -6,7 +6,7 @@
|
||||
* ==============================================================================
|
||||
*/
|
||||
const fs = require("fs");
|
||||
// const handleNodemailer = require("./handleNodemailer");
|
||||
const { IncomingMessage } = require("http");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
@ -24,6 +24,7 @@ const fs = require("fs");
|
||||
* message: string,
|
||||
* component?: string,
|
||||
* noMail?: boolean,
|
||||
* req?: import("next").NextApiRequest & IncomingMessage,
|
||||
* }} params - user id
|
||||
*
|
||||
* @returns {Promise<void>}
|
||||
@ -33,21 +34,68 @@ module.exports = async function serverError({
|
||||
message,
|
||||
component,
|
||||
noMail,
|
||||
req,
|
||||
}) {
|
||||
const log = `🚀 SERVER ERROR ===========================\nUser Id: ${
|
||||
user?.id
|
||||
}\nUser Name: ${user?.first_name} ${user?.last_name}\nUser Email: ${
|
||||
user?.email
|
||||
}\nError Message: ${message}\nComponent: ${component}\nDate: ${Date()}\n========================================`;
|
||||
const date = new Date();
|
||||
|
||||
if (!fs.existsSync(`./.tmp/error.log`)) {
|
||||
fs.writeFileSync(`./.tmp/error.log`, "", "utf-8");
|
||||
const reqIp = (() => {
|
||||
if (!req) return null;
|
||||
try {
|
||||
const forwarded = req.headers["x-forwarded-for"];
|
||||
const realIp = req.headers["x-real-ip"];
|
||||
const cloudflareIp = req.headers["cf-connecting-ip"];
|
||||
|
||||
// Convert forwarded IPs to string and get the first IP if multiple exist
|
||||
const forwardedIp = Array.isArray(forwarded)
|
||||
? forwarded[0]
|
||||
: forwarded?.split(",")[0];
|
||||
|
||||
const clientIp =
|
||||
cloudflareIp ||
|
||||
forwardedIp ||
|
||||
realIp ||
|
||||
req.socket.remoteAddress;
|
||||
if (!clientIp) return null;
|
||||
|
||||
return String(clientIp);
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
})();
|
||||
|
||||
try {
|
||||
let log = `🚀 SERVER ERROR ===========================\nError Message: ${message}\nComponent: ${component}`;
|
||||
|
||||
if (user?.id && user?.first_name && user?.last_name && user?.email) {
|
||||
log += `\nUser Id: ${user?.id}\nUser Name: ${user?.first_name} ${user?.last_name}\nUser Email: ${user?.email}`;
|
||||
}
|
||||
|
||||
if (req?.url) {
|
||||
log += `\nURL: ${req.url}`;
|
||||
}
|
||||
|
||||
if (req?.body) {
|
||||
log += `\nRequest Body: ${JSON.stringify(req.body, null, 4)}`;
|
||||
}
|
||||
|
||||
if (reqIp) {
|
||||
log += `\nIP: ${reqIp}`;
|
||||
}
|
||||
|
||||
log += `\nDate: ${date.toDateString()}`;
|
||||
log += "\n========================================";
|
||||
|
||||
if (!fs.existsSync(`./.tmp/error.log`)) {
|
||||
fs.writeFileSync(`./.tmp/error.log`, "", "utf-8");
|
||||
}
|
||||
|
||||
const initialText = fs.readFileSync(`./.tmp/error.log`, "utf-8");
|
||||
|
||||
fs.writeFileSync(`./.tmp/error.log`, log);
|
||||
fs.appendFileSync(`./.tmp/error.log`, `\n\n\n\n\n${initialText}`);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log("Server Error Reporting Error:", error.message);
|
||||
}
|
||||
|
||||
const initialText = fs.readFileSync(`./.tmp/error.log`, "utf-8");
|
||||
|
||||
fs.writeFileSync(`./.tmp/error.log`, log);
|
||||
fs.appendFileSync(`./.tmp/error.log`, `\n\n\n\n\n${initialText}`);
|
||||
};
|
||||
|
||||
////////////////////////////////////////
|
||||
|
49
package-shared/functions/backend/setUserSchemaData.js
Executable file
49
package-shared/functions/backend/setUserSchemaData.js
Executable file
@ -0,0 +1,49 @@
|
||||
// @ts-check
|
||||
|
||||
const serverError = require("./serverError");
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* ==============================================================================
|
||||
* @param {Object} params
|
||||
* @param {string | number} params.userId
|
||||
* @param {import("../../types").DSQL_DatabaseSchemaType[]} params.schemaData
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export default function setUserSchemaData({ userId, schemaData }) {
|
||||
try {
|
||||
const userSchemaFilePath = path.resolve(
|
||||
process.cwd(),
|
||||
`${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${userId}/main.json`
|
||||
);
|
||||
fs.writeFileSync(
|
||||
userSchemaFilePath,
|
||||
JSON.stringify(schemaData),
|
||||
"utf8"
|
||||
);
|
||||
|
||||
return true;
|
||||
} catch (/** @type {any} */ error) {
|
||||
serverError({
|
||||
component: "/functions/backend/setUserSchemaData",
|
||||
message: error.message,
|
||||
});
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
@ -1,8 +1,9 @@
|
||||
// @ts-check
|
||||
|
||||
const { IncomingMessage } = require("http");
|
||||
const decrypt = require("./decrypt");
|
||||
const parseCookies = require("../../utils/backend/parseCookies");
|
||||
const decrypt = require("../dsql/decrypt");
|
||||
const getAuthCookieNames = require("./cookies/get-auth-cookie-names");
|
||||
|
||||
/**
|
||||
* @async
|
||||
@ -11,14 +12,18 @@ const parseCookies = require("../../utils/backend/parseCookies");
|
||||
* @returns {Promise<({ email: string, password: string, authKey: string, logged_in_status: boolean, date: number } | null)>}
|
||||
*/
|
||||
module.exports = async function (req) {
|
||||
const { keyCookieName, csrfCookieName } = getAuthCookieNames();
|
||||
const suKeyName = `${keyCookieName}_su`;
|
||||
|
||||
const cookies = parseCookies({ request: req });
|
||||
/** ********************* Check for existence of required cookie */
|
||||
if (!cookies?.datasquirelSuAdminUserAuthKey) {
|
||||
if (!cookies?.[suKeyName]) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** ********************* Grab the payload */
|
||||
let userPayload = decrypt(cookies.datasquirelSuAdminUserAuthKey);
|
||||
let userPayload = decrypt({
|
||||
encryptedString: cookies[suKeyName],
|
||||
});
|
||||
|
||||
/** ********************* Return if no payload */
|
||||
if (!userPayload) return null;
|
||||
|
@ -4,11 +4,10 @@ const { scryptSync, createDecipheriv } = require("crypto");
|
||||
const { Buffer } = require("buffer");
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.encryptedString
|
||||
* @param {string} param0.encryptionKey
|
||||
* @param {string} param0.encryptionSalt
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns
|
||||
*/
|
||||
const decrypt = ({ encryptedString, encryptionKey, encryptionSalt }) => {
|
||||
@ -17,19 +16,27 @@ const decrypt = ({ encryptedString, encryptionKey, encryptionSalt }) => {
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
if (!encryptionKey?.match(/.{8,}/)) {
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
|
||||
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
|
||||
: 24;
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
console.log("Decrption key is invalid");
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
if (!encryptionSalt?.match(/.{8,}/)) {
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
console.log("Decrption salt is invalid");
|
||||
return encryptedString;
|
||||
}
|
||||
|
||||
const algorithm = "aes-192-cbc";
|
||||
|
||||
let key = scryptSync(encryptionKey, encryptionSalt, 24);
|
||||
let key = scryptSync(finalEncryptionKey, finalEncryptionSalt, finalKeyLen);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
const decipher = createDecipheriv(algorithm, key, iv);
|
@ -7,8 +7,8 @@ const { Buffer } = require("buffer");
|
||||
*
|
||||
* @param {object} param0
|
||||
* @param {string} param0.data
|
||||
* @param {string} param0.encryptionKey
|
||||
* @param {string} param0.encryptionSalt
|
||||
* @param {string} [param0.encryptionKey]
|
||||
* @param {string} [param0.encryptionSalt]
|
||||
* @returns {string | null}
|
||||
*/
|
||||
const encrypt = ({ data, encryptionKey, encryptionSalt }) => {
|
||||
@ -16,19 +16,28 @@ const encrypt = ({ data, encryptionKey, encryptionSalt }) => {
|
||||
console.log("Encryption string is invalid");
|
||||
return data;
|
||||
}
|
||||
if (!encryptionKey?.match(/.{8,}/)) {
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
const finalEncryptionSalt =
|
||||
encryptionSalt || process.env.DSQL_ENCRYPTION_SALT;
|
||||
const finalKeyLen = process.env.DSQL_ENCRYPTION_KEY_LENGTH
|
||||
? Number(process.env.DSQL_ENCRYPTION_KEY_LENGTH)
|
||||
: 24;
|
||||
|
||||
if (!finalEncryptionKey?.match(/.{8,}/)) {
|
||||
console.log("Encryption key is invalid");
|
||||
return data;
|
||||
}
|
||||
if (!encryptionSalt?.match(/.{8,}/)) {
|
||||
if (!finalEncryptionSalt?.match(/.{8,}/)) {
|
||||
console.log("Encryption salt is invalid");
|
||||
return data;
|
||||
}
|
||||
|
||||
const algorithm = "aes-192-cbc";
|
||||
const password = encryptionKey;
|
||||
const password = finalEncryptionKey;
|
||||
|
||||
let key = scryptSync(password, encryptionSalt, 24);
|
||||
let key = scryptSync(password, finalEncryptionSalt, finalKeyLen);
|
||||
let iv = Buffer.alloc(16, 0);
|
||||
// @ts-ignore
|
||||
const cipher = createCipheriv(algorithm, key, iv);
|
10
package-shared/functions/dsql/sql/sql-generator.d.ts
vendored
Normal file
10
package-shared/functions/dsql/sql/sql-generator.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
export = sqlGenerator;
|
||||
declare function sqlGenerator(Param0: {
|
||||
genObject?: import("../../../types").ServerQueryParam;
|
||||
tableName: string;
|
||||
}):
|
||||
| {
|
||||
string: string;
|
||||
values: string[];
|
||||
}
|
||||
| undefined;
|
@ -3,7 +3,7 @@
|
||||
/**
|
||||
* # SQL Query Generator
|
||||
* @description Generates an SQL Query for node module `mysql` or `serverless-mysql`
|
||||
* @type {import("../../package-shared/types").SqlGeneratorFn}
|
||||
* @type {import("../../../types").SqlGeneratorFn}
|
||||
*/
|
||||
function sqlGenerator({ tableName, genObject }) {
|
||||
if (!genObject) return undefined;
|
||||
@ -62,8 +62,8 @@ function sqlGenerator({ tableName, genObject }) {
|
||||
});
|
||||
|
||||
function generateJoinStr(
|
||||
/** @type {import("../../package-shared/types").ServerQueryParamsJoinMatchObject} */ mtch,
|
||||
/** @type {import("../../package-shared/types").ServerQueryParamsJoin} */ join
|
||||
/** @type {import("../../../types").ServerQueryParamsJoinMatchObject} */ mtch,
|
||||
/** @type {import("../../../types").ServerQueryParamsJoin} */ join
|
||||
) {
|
||||
return `${
|
||||
typeof mtch.source == "object" ? mtch.source.tableName : tableName
|
57
package-shared/shell/checkDb.js
Executable file
57
package-shared/shell/checkDb.js
Executable file
@ -0,0 +1,57 @@
|
||||
// @ts-check
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
const mysql = require("serverless-mysql");
|
||||
const grabDbSSL = require("../utils/backend/grabDbSSL");
|
||||
|
||||
const connection = mysql({
|
||||
config: {
|
||||
host: process.env.DSQL_DB_HOST,
|
||||
user: process.env.DSQL_DB_USERNAME,
|
||||
password: process.env.DSQL_DB_PASSWORD,
|
||||
database: process.env.DSQL_DB_NAME,
|
||||
charset: "utf8mb4",
|
||||
ssl: grabDbSSL(),
|
||||
},
|
||||
});
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* # Main DB Handler Function
|
||||
* @async
|
||||
*
|
||||
* @param {object} params
|
||||
* @param {string} params.query
|
||||
* @param {string[] | object} [params.values]
|
||||
* @param {string} [params.database]
|
||||
*
|
||||
* @returns {Promise<object|null>}
|
||||
*/
|
||||
(async () => {
|
||||
/**
|
||||
* Switch Database
|
||||
*
|
||||
* @description If a database is provided, switch to it
|
||||
*/
|
||||
try {
|
||||
const result = await connection.query(
|
||||
"SELECT id,first_name,last_name FROM users LIMIT 3"
|
||||
);
|
||||
console.log("Connection Query Success =>", result);
|
||||
} catch (/** @type {any} */ error) {
|
||||
console.log("Connection query ERROR =>", error.message);
|
||||
} finally {
|
||||
connection.end();
|
||||
process.exit();
|
||||
}
|
||||
})();
|
302
package-shared/shell/createDbFromSchema.js
Executable file
302
package-shared/shell/createDbFromSchema.js
Executable file
@ -0,0 +1,302 @@
|
||||
// @ts-check
|
||||
|
||||
const path = require("path");
|
||||
const fs = require("fs");
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
const noDatabaseDbHandler = require("./utils/noDatabaseDbHandler");
|
||||
const varDatabaseDbHandler = require("./utils/varDatabaseDbHandler");
|
||||
const createTable = require("./utils/createTable");
|
||||
const updateTable = require("./utils/updateTable");
|
||||
const dbHandler = require("./utils/dbHandler");
|
||||
const EJSON = require("../utils/ejson");
|
||||
|
||||
const execFlag = process.argv.find((arg) => arg === "--exec");
|
||||
|
||||
/**
|
||||
* Create database from Schema Function
|
||||
* ==============================================================================
|
||||
* @param {object} params - Single object params
|
||||
* @param {number|string|null} [params.userId] - User ID or null
|
||||
* @param {string} [params.targetDatabase] - User Database full name
|
||||
* @param {import("../types").DSQL_DatabaseSchemaType[]} [params.dbSchemaData]
|
||||
*/
|
||||
async function createDbFromSchema({ userId, targetDatabase, dbSchemaData }) {
|
||||
const schemaPath = userId
|
||||
? path.join(
|
||||
String(process.env.DSQL_USER_DB_SCHEMA_PATH),
|
||||
`/user-${userId}/main.json`
|
||||
)
|
||||
: path.resolve(__dirname, "../../jsonData/dbSchemas/main.json");
|
||||
|
||||
/** @type {import("../types").DSQL_DatabaseSchemaType[] | undefined} */
|
||||
const dbSchema =
|
||||
dbSchemaData ||
|
||||
/** @type {import("../types").DSQL_DatabaseSchemaType[] | undefined} */ (
|
||||
EJSON.parse(fs.readFileSync(schemaPath, "utf8"))
|
||||
);
|
||||
|
||||
if (!dbSchema) {
|
||||
console.log("Schema Not Found!");
|
||||
return;
|
||||
}
|
||||
|
||||
// await createDatabasesFromSchema(dbSchema);
|
||||
|
||||
for (let i = 0; i < dbSchema.length; i++) {
|
||||
/** @type {import("../types").DSQL_DatabaseSchemaType} */
|
||||
const database = dbSchema[i];
|
||||
const { dbFullName, tables, dbName, dbSlug, childrenDatabases } =
|
||||
database;
|
||||
|
||||
if (targetDatabase && dbFullName != targetDatabase) {
|
||||
continue;
|
||||
}
|
||||
|
||||
/** @type {any} */
|
||||
const dbCheck = await noDatabaseDbHandler(
|
||||
`SELECT SCHEMA_NAME AS dbFullName FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '${dbFullName}'`
|
||||
);
|
||||
|
||||
if (dbCheck && dbCheck[0]?.dbFullName) {
|
||||
// Database Exists
|
||||
} else {
|
||||
const newDatabase = await noDatabaseDbHandler(
|
||||
`CREATE DATABASE IF NOT EXISTS \`${dbFullName}\` CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Select all tables
|
||||
* @type {any}
|
||||
* @description Select All tables in target database
|
||||
*/
|
||||
const allTables = await noDatabaseDbHandler(
|
||||
`SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='${dbFullName}'`
|
||||
);
|
||||
|
||||
// let tableDropped;
|
||||
|
||||
for (let tb = 0; tb < allTables.length; tb++) {
|
||||
const { TABLE_NAME } = allTables[tb];
|
||||
|
||||
/**
|
||||
* @description Check if TABLE_NAME is part of the tables contained
|
||||
* in the user schema JSON. If it's not, the table is either deleted
|
||||
* or the table name has been recently changed
|
||||
*/
|
||||
if (
|
||||
!tables.filter((_table) => _table.tableName === TABLE_NAME)[0]
|
||||
) {
|
||||
const oldTableFilteredArray = tables.filter(
|
||||
(_table) =>
|
||||
_table.tableNameOld &&
|
||||
_table.tableNameOld === TABLE_NAME
|
||||
);
|
||||
|
||||
/**
|
||||
* @description Check if this table has been recently renamed. Rename
|
||||
* table id true. Drop table if false
|
||||
*/
|
||||
if (oldTableFilteredArray && oldTableFilteredArray[0]) {
|
||||
console.log("Renaming Table");
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `RENAME TABLE \`${oldTableFilteredArray[0].tableNameOld}\` TO \`${oldTableFilteredArray[0].tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
} else {
|
||||
console.log(`Dropping Table from ${dbFullName}`);
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `DROP TABLE \`${TABLE_NAME}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
const deleteTableEntry = await dbHandler({
|
||||
query: `DELETE FROM user_database_tables WHERE user_id = ? AND db_slug = ? AND table_slug = ?`,
|
||||
values: [userId, dbSlug, TABLE_NAME],
|
||||
database: "datasquirel",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const recordedDbEntryArray = userId
|
||||
? await varDatabaseDbHandler({
|
||||
database: "datasquirel",
|
||||
queryString: `SELECT * FROM user_databases WHERE db_full_name = ?`,
|
||||
queryValuesArray: [dbFullName],
|
||||
})
|
||||
: undefined;
|
||||
|
||||
const recordedDbEntry = recordedDbEntryArray?.[0];
|
||||
|
||||
/**
|
||||
* @description Iterate through each table and perform table actions
|
||||
*/
|
||||
for (let t = 0; t < tables.length; t++) {
|
||||
const table = tables[t];
|
||||
|
||||
const { tableName, fields, indexes } = table;
|
||||
|
||||
/**
|
||||
* @description Check if table exists
|
||||
* @type {any}
|
||||
*/
|
||||
const tableCheck = await varDatabaseDbHandler({
|
||||
queryString: `
|
||||
SELECT EXISTS (
|
||||
SELECT
|
||||
TABLE_NAME
|
||||
FROM
|
||||
information_schema.TABLES
|
||||
WHERE
|
||||
TABLE_SCHEMA = ? AND
|
||||
TABLE_NAME = ?
|
||||
) AS tableExists`,
|
||||
queryValuesArray: [dbFullName, table.tableName],
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
if (tableCheck && tableCheck[0]?.tableExists > 0) {
|
||||
/**
|
||||
* @description Update table if table exists
|
||||
*/
|
||||
const updateExistingTable = await updateTable({
|
||||
dbFullName: dbFullName,
|
||||
tableName: tableName,
|
||||
tableNameFull: table.tableFullName,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
tableIndex: t,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
});
|
||||
|
||||
if (table.childrenTables && table.childrenTables[0]) {
|
||||
for (let ch = 0; ch < table.childrenTables.length; ch++) {
|
||||
const childTable = table.childrenTables[ch];
|
||||
|
||||
const updateExistingChildTable = await updateTable({
|
||||
dbFullName: childTable.dbNameFull,
|
||||
tableName: childTable.tableName,
|
||||
tableNameFull: childTable.tableNameFull,
|
||||
tableInfoArray: fields,
|
||||
userId,
|
||||
dbSchema,
|
||||
tableIndexes: indexes,
|
||||
clone: true,
|
||||
childDb: database.childDatabase || undefined,
|
||||
recordedDbEntry,
|
||||
tableSchema: table,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////
|
||||
} else {
|
||||
////////////////////////////////////////
|
||||
|
||||
/**
|
||||
* @description Create new Table if table doesnt exist
|
||||
*/
|
||||
const createNewTable = await createTable({
|
||||
tableName: tableName,
|
||||
tableInfoArray: fields,
|
||||
dbFullName: dbFullName,
|
||||
dbSchema,
|
||||
tableSchema: table,
|
||||
recordedDbEntry,
|
||||
});
|
||||
|
||||
if (indexes && indexes[0]) {
|
||||
/**
|
||||
* Handle DATASQUIREL Table Indexes
|
||||
* ===================================================
|
||||
* @description Iterate through each datasquirel schema
|
||||
* table index(if available), and perform operations
|
||||
*/
|
||||
if (indexes && indexes[0]) {
|
||||
for (let g = 0; g < indexes.length; g++) {
|
||||
const {
|
||||
indexType,
|
||||
indexName,
|
||||
indexTableFields,
|
||||
alias,
|
||||
} = indexes[g];
|
||||
|
||||
if (!alias?.match(/./)) continue;
|
||||
|
||||
/**
|
||||
* @description Check for existing Index in MYSQL db
|
||||
*/
|
||||
try {
|
||||
/**
|
||||
* @type {import("../types").DSQL_MYSQL_SHOW_INDEXES_Type[]}
|
||||
* @description All indexes from MYSQL db
|
||||
*/ // @ts-ignore
|
||||
const allExistingIndexes =
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `SHOW INDEXES FROM \`${tableName}\``,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
const existingKeyInDb =
|
||||
allExistingIndexes.filter(
|
||||
(indexObject) =>
|
||||
indexObject.Key_name === alias
|
||||
);
|
||||
if (!existingKeyInDb[0])
|
||||
throw new Error(
|
||||
"This Index Does not Exist"
|
||||
);
|
||||
} catch (error) {
|
||||
/**
|
||||
* @description Create new index if determined that it
|
||||
* doesn't exist in MYSQL db
|
||||
*/
|
||||
await varDatabaseDbHandler({
|
||||
queryString: `CREATE${
|
||||
indexType?.match(/fullText/i)
|
||||
? " FULLTEXT"
|
||||
: ""
|
||||
} INDEX \`${alias}\` ON ${tableName}(${indexTableFields
|
||||
?.map((nm) => nm.value)
|
||||
.map((nm) => `\`${nm}\``)
|
||||
.join(",")}) COMMENT 'schema_index'`,
|
||||
database: dbFullName,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Check all children databases
|
||||
*/
|
||||
if (childrenDatabases?.[0]) {
|
||||
for (let ch = 0; ch < childrenDatabases.length; ch++) {
|
||||
const childDb = childrenDatabases[ch];
|
||||
const { dbFullName } = childDb;
|
||||
|
||||
await createDbFromSchema({
|
||||
userId,
|
||||
targetDatabase: dbFullName,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = createDbFromSchema;
|
||||
|
||||
if (execFlag) {
|
||||
createDbFromSchema({});
|
||||
}
|
7
package-shared/shell/deploy.js
Executable file
7
package-shared/shell/deploy.js
Executable file
@ -0,0 +1,7 @@
|
||||
// @ts-check
|
||||
|
||||
const fs = require("fs");
|
||||
|
||||
async function deploy() {}
|
||||
|
||||
deploy();
|
58
package-shared/shell/encodingUpdate.js
Executable file
58
package-shared/shell/encodingUpdate.js
Executable file
@ -0,0 +1,58 @@
|
||||
// @ts-check
|
||||
|
||||
require("dotenv").config({ path: "./../.env" });
|
||||
|
||||
////////////////////////////////////////
|
||||
|
||||
const varDatabaseDbHandler = require("../functions/backend/varDatabaseDbHandler");
|
||||
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
/** ****************************************************************************** */
|
||||
|
||||
/**
|
||||
* Grab Schema
|
||||
*
|
||||
* @description Grab Schema
|
||||
*/
|
||||
varDatabaseDbHandler({
|
||||
queryString: `SELECT user_database_tables.*,user_databases.db_full_name FROM user_database_tables JOIN user_databases ON user_database_tables.db_id=user_databases.id`,
|
||||
database: "datasquirel",
|
||||
}).then(async (tables) => {
|
||||
for (let i = 0; i < tables.length; i++) {
|
||||
const table = tables[i];
|
||||
const {
|
||||
id,
|
||||
user_id,
|
||||
db_id,
|
||||
db_full_name,
|
||||
table_name,
|
||||
table_slug,
|
||||
table_description,
|
||||
} = table;
|
||||
|
||||
const tableInfo = await varDatabaseDbHandler({
|
||||
queryString: `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA='${db_full_name}' AND TABLE_NAME='${table_slug}'`,
|
||||
database: db_full_name,
|
||||
});
|
||||
|
||||
const updateDbCharset = await varDatabaseDbHandler({
|
||||
queryString: `ALTER DATABASE ${db_full_name} CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin;`,
|
||||
database: db_full_name,
|
||||
});
|
||||
|
||||
const updateEncoding = await varDatabaseDbHandler({
|
||||
queryString: `ALTER TABLE \`${table_slug}\` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_bin`,
|
||||
database: db_full_name,
|
||||
});
|
||||
}
|
||||
|
||||
process.exit();
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue
Block a user