This commit is contained in:
Benjamin Toby
2024-12-06 11:31:24 +01:00
parent 6df20790f4
commit 8ca2779741
153 changed files with 6621 additions and 3899 deletions
+14 -28
View File
@@ -1,15 +1,13 @@
// @ts-check
const https = require("https");
const http = require("http");
const path = require("path");
const fs = require("fs");
const localAddUser = require("../engine/user/add-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiCreateUser = require("../package-shared/functions/api/users/api-create-user");
/**
* Add User to Database
* ==============================================================================
* ============================================
* @async
*
* @param {object} param - Single object passed
@@ -18,7 +16,9 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {import("../package-shared/types").UserDataPayload} param.payload - User Data Payload
* @param {string} param.encryptionKey
* @param {string} [param.encryptionSalt]
* @param {boolean} [param.user_id] - User ID
* @param {string | number} [param.user_id]
* @param {string | number} param.apiUserId
* @param {boolean} [param.useLocal]
*
* @returns { Promise<import("../package-shared/types").AddUserFunctionReturn> }
*/
@@ -27,23 +27,16 @@ async function addUser({
payload,
database,
encryptionKey,
encryptionSalt,
user_id,
useLocal,
apiUserId,
}) {
/**
* Check for local DB settings
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const {
DSQL_HOST,
DSQL_USER,
DSQL_PASS,
DSQL_DB_NAME,
DSQL_KEY,
DSQL_REF_DB_NAME,
DSQL_FULL_SYNC,
} = process.env;
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -52,7 +45,8 @@ async function addUser({
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
@@ -66,11 +60,11 @@ async function addUser({
} catch (error) {}
if (dbSchema) {
return await localAddUser({
dbSchema: dbSchema,
payload: payload,
return await apiCreateUser({
database: DSQL_DB_NAME,
encryptionKey,
encryptionSalt,
payload,
userId: apiUserId,
});
}
}
@@ -130,15 +124,7 @@ async function addUser({
httpsRequest.end();
});
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = addUser;
+1 -1
View File
@@ -6,7 +6,7 @@
* ==============================================================================
*/
const http = require("http");
const decrypt = require("../functions/decrypt");
const decrypt = require("../package-shared/functions/dsql/decrypt");
const parseCookies = require("../utils/functions/parseCookies");
/** ****************************************************************************** */
-27
View File
@@ -1,27 +0,0 @@
export = getUser;
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/**
* ==============================================================================
* Main Function
* ==============================================================================
* @async
*
* @param {object} params - Single Param object containing params
* @param {String} params.key - API Key
* @param {String} params.database - Target Database
* @param {number} params.userId - user id
* @param {string[]} [params.fields] - fields to select
*
* @returns { Promise<import("../types/user.td").GetUserFunctionReturn>}
*/
declare function getUser({ key, userId, database, fields }: {
key: string;
database: string;
userId: number;
fields?: string[];
}): Promise<any>;
+8 -14
View File
@@ -9,8 +9,8 @@ const path = require("path");
const fs = require("fs");
const https = require("https");
const http = require("http");
const getLocalUser = require("../engine/user/get-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiGetUser = require("../package-shared/functions/api/users/api-get-user");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -31,10 +31,11 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {number} params.userId - user id
* @param {string[]} [params.fields] - fields to select
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
*
* @returns { Promise<import("../package-shared/types").GetUserFunctionReturn>}
*/
async function getUser({ key, userId, database, fields, user_id }) {
async function getUser({ key, userId, database, fields, user_id, useLocal }) {
/**
* Initialize
*/
@@ -72,21 +73,14 @@ async function getUser({ key, userId, database, fields, user_id }) {
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const {
DSQL_HOST,
DSQL_USER,
DSQL_PASS,
DSQL_DB_NAME,
DSQL_KEY,
DSQL_REF_DB_NAME,
DSQL_FULL_SYNC,
} = process.env;
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
@@ -100,10 +94,10 @@ async function getUser({ key, userId, database, fields, user_id }) {
} catch (error) {}
if (dbSchema) {
return await getLocalUser({
return await apiGetUser({
userId,
fields: [...new Set(updatedFields)],
dbSchema,
dbFullName: DSQL_DB_NAME,
});
}
}
+30 -34
View File
@@ -9,16 +9,9 @@ const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../functions/encrypt");
const loginLocalUser = require("../engine/user/login-user");
const encrypt = require("../package-shared/functions/dsql/encrypt");
const grabHostNames = require("../package-shared/utils/grab-host-names");
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
/** ****************************************************************************** */
const apiLoginUser = require("../package-shared/functions/api/users/api-login");
/**
* Login A user
@@ -42,6 +35,8 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {string} [params.temp_code_field] - Database table field name for temporary code
* @param {boolean} [params.token] - Send access key as part of response body?
* @param {boolean} [params.user_id] - User ID
* @param {boolean} [params.skipPassword]
* @param {boolean} [params.useLocal]
*
* @returns { Promise<import("../package-shared/types").AuthenticatedUser>}
*/
@@ -58,6 +53,8 @@ async function loginUser({
temp_code_field,
token,
user_id,
skipPassword,
useLocal,
}) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -69,6 +66,19 @@ async function loginUser({
: defaultTempLoginFieldName
: undefined;
/**
* Check required fields
*
* @description Check required fields
*/
if (!payload.email) {
return {
success: false,
payload: null,
msg: "Email Required",
};
}
/**
* Check Encryption Keys
*
@@ -112,21 +122,14 @@ async function loginUser({
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const {
DSQL_HOST,
DSQL_USER,
DSQL_PASS,
DSQL_DB_NAME,
DSQL_KEY,
DSQL_REF_DB_NAME,
DSQL_FULL_SYNC,
} = process.env;
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
@@ -140,10 +143,14 @@ async function loginUser({
} catch (error) {}
if (dbSchema) {
httpResponse = await loginLocalUser({
payload,
httpResponse = await apiLoginUser({
database: process.env.DSQL_DB_NAME || "",
email: payload.email,
username: payload.username,
password: payload.password,
skipPassword,
encryptionKey,
additionalFields,
dbSchema,
email_login,
email_login_code,
email_login_field: emailLoginTempCodeFieldName,
@@ -169,6 +176,7 @@ async function loginUser({
email_login_code,
email_login_field: emailLoginTempCodeFieldName,
token,
skipPassword: skipPassword,
};
const reqPayloadJSON = JSON.stringify(reqPayload);
@@ -218,10 +226,6 @@ async function loginUser({
});
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
/**
* Make https request
*
@@ -250,15 +254,7 @@ async function loginUser({
]);
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
return httpResponse;
}
/** ********************************************** */
/** ********************************************** */
/** ********************************************** */
module.exports = loginUser;
+4 -4
View File
@@ -9,11 +9,11 @@ const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../functions/encrypt");
const encrypt = require("../package-shared/functions/dsql/encrypt");
const userAuth = require("./user-auth");
const localReauthUser = require("../engine/user/reauth-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiReauthUser = require("../package-shared/functions/api/users/api-reauth-user");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -117,10 +117,10 @@ async function reauthUser({
} catch (error) {}
if (dbSchema) {
httpResponse = await localReauthUser({
httpResponse = await apiReauthUser({
existingUser: existingUser.payload,
additionalFields,
dbSchema,
database: DSQL_DB_NAME,
});
}
} else {
+11 -13
View File
@@ -9,10 +9,8 @@ const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../functions/encrypt");
const loginLocalUser = require("../engine/user/login-user");
const localSendEmailCode = require("../engine/user/send-email-code");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiSendEmailCode = require("../package-shared/functions/api/users/api-send-email-code");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -65,6 +63,11 @@ async function sendEmailCode({
? temp_code_field
: defaultTempLoginFieldName;
const emailHtml = fs.readFileSync(
path.resolve(__dirname, "../package-shared/html/one-time-code.html"),
"utf-8"
);
/**
* Check Encryption Keys
*
@@ -128,14 +131,15 @@ async function sendEmailCode({
} catch (error) {}
if (dbSchema) {
httpResponse = await localSendEmailCode({
httpResponse = await apiSendEmailCode({
database: DSQL_DB_NAME,
email,
dbSchema,
email_login_field: emailLoginTempCodeFieldName,
html: emailHtml,
mail_domain,
mail_password,
mail_username,
mail_port,
mail_username,
sender,
});
}
@@ -157,13 +161,7 @@ async function sendEmailCode({
mail_username,
mail_port,
sender,
html: fs.readFileSync(
path.resolve(
__dirname,
"../engine/user/one-time-code.html"
),
"utf-8"
),
html: emailHtml,
});
const httpsRequest = scheme.request(
+4 -4
View File
@@ -9,9 +9,9 @@ const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../functions/encrypt");
const localGithubAuth = require("../../engine/user/social/github-auth");
const encrypt = require("../../package-shared/functions/dsql/encrypt");
const grabHostNames = require("../../package-shared/utils/grab-host-names");
const apiGithubLogin = require("../../package-shared/functions/api/users/social/api-github-login");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -168,14 +168,14 @@ async function githubAuth({
} catch (error) {}
if (dbSchema) {
httpResponse = await localGithubAuth({
dbSchema: dbSchema,
httpResponse = await apiGithubLogin({
code,
email: email || undefined,
clientId,
clientSecret,
additionalFields,
res: response,
database: DSQL_DB_NAME,
});
}
} else {
+14 -16
View File
@@ -9,9 +9,9 @@ const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../functions/encrypt");
const localGoogleAuth = require("../../engine/user/social/google-auth");
const encrypt = require("../../package-shared/functions/dsql/encrypt");
const grabHostNames = require("../../package-shared/utils/grab-host-names");
const apiGoogleLogin = require("../../package-shared/functions/api/users/social/api-google-login");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -44,6 +44,8 @@ const grabHostNames = require("../../package-shared/utils/grab-host-names");
* @param {string} params.encryptionSalt - Encryption salt
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
* @param {boolean} [params.user_id] - User ID
* @param {string | number} [params.apiUserID] - Required for Local
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
*
* @returns { Promise<FunctionReturn> }
*/
@@ -57,6 +59,8 @@ async function googleAuth({
encryptionSalt,
additionalFields,
user_id,
apiUserID,
useLocal,
}) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
@@ -136,21 +140,14 @@ async function googleAuth({
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const {
DSQL_HOST,
DSQL_USER,
DSQL_PASS,
DSQL_DB_NAME,
DSQL_KEY,
DSQL_REF_DB_NAME,
DSQL_FULL_SYNC,
} = process.env;
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME } = process.env;
if (
DSQL_HOST?.match(/./) &&
DSQL_USER?.match(/./) &&
DSQL_PASS?.match(/./) &&
DSQL_DB_NAME?.match(/./)
DSQL_DB_NAME?.match(/./) &&
useLocal
) {
/** @type {import("../../package-shared/types").DSQL_DatabaseSchemaType | undefined | undefined} */
let dbSchema;
@@ -163,13 +160,14 @@ async function googleAuth({
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
if (dbSchema) {
httpResponse = await localGoogleAuth({
dbSchema: dbSchema,
if (dbSchema && apiUserID) {
httpResponse = await apiGoogleLogin({
token,
clientId,
additionalFields,
response: response,
res: response,
database: DSQL_DB_NAME,
userId: apiUserID,
});
}
} else {
+3 -3
View File
@@ -4,8 +4,8 @@ const http = require("http");
const https = require("https");
const path = require("path");
const fs = require("fs");
const localUpdateUser = require("../engine/user/update-user");
const grabHostNames = require("../package-shared/utils/grab-host-names");
const apiUpdateUser = require("../package-shared/functions/api/users/api-update-user");
/**
* # Update User
@@ -56,9 +56,9 @@ async function updateUser({ key, payload, database, user_id }) {
} catch (error) {}
if (dbSchema) {
return await localUpdateUser({
dbSchema: dbSchema,
return await apiUpdateUser({
payload: payload,
dbFullName: DSQL_DB_NAME,
});
}
}
+1 -1
View File
@@ -6,7 +6,7 @@
* ==============================================================================
*/
const http = require("http");
const decrypt = require("../functions/decrypt");
const decrypt = require("../package-shared/functions/dsql/decrypt");
const parseCookies = require("../utils/functions/parseCookies");
/** ****************************************************************************** */
+1 -1
View File
@@ -6,7 +6,7 @@
* ==============================================================================
*/
const http = require("http");
const decrypt = require("../functions/decrypt");
const decrypt = require("../package-shared/functions/dsql/decrypt");
/** ****************************************************************************** */
/** ****************************************************************************** */