Refactor types

This commit is contained in:
Benjamin Toby
2024-10-14 07:49:01 +01:00
parent 11a4c1f4d9
commit d84b15c35c
46 changed files with 696 additions and 376 deletions
+21 -4
View File
@@ -36,9 +36,17 @@ const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
* @param {string} params.clientId
* @param {string} params.clientSecret
* @param {object} [params.additionalFields]
* @param {import("../../../types/database-schema.td").DSQL_DatabaseSchemaType} params.dbSchema
* @param {import("@/package-shared/types/database-schema.td").DSQL_DatabaseSchemaType} params.dbSchema
*/
async function localGithubAuth({ res, code, email, clientId, clientSecret, additionalFields, dbSchema }) {
async function localGithubAuth({
res,
code,
email,
clientId,
clientSecret,
additionalFields,
dbSchema,
}) {
try {
/**
* User auth
@@ -52,7 +60,12 @@ async function localGithubAuth({ res, code, email, clientId, clientSecret, addit
};
}
if (typeof code !== "string" || typeof clientId !== "string" || typeof clientSecret !== "string" || typeof database !== "string") {
if (
typeof code !== "string" ||
typeof clientId !== "string" ||
typeof clientSecret !== "string" ||
typeof database !== "string"
) {
return {
success: false,
msg: "Wrong Parameters",
@@ -81,7 +94,11 @@ async function localGithubAuth({ res, code, email, clientId, clientSecret, addit
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 nameArray = targetName?.match(/ /)
? targetName?.split(" ")
: targetName?.match(/\-/)
? targetName?.split("-")
: [targetName];
const payload = {
email: gitHubUser.email,
+1 -1
View File
@@ -44,7 +44,7 @@ const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
* @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("../../../types/database-schema.td").DSQL_DatabaseSchemaType} [params.dbSchema] - Database Schema
* @param {import("@/package-shared/types/database-schema.td").DSQL_DatabaseSchemaType} [params.dbSchema] - Database Schema
*
* @returns { Promise<FunctionReturn> }
*/
+37 -8
View File
@@ -74,13 +74,24 @@ const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
* res: http.ServerResponse,
* supEmail?: string | null,
* additionalFields?: object,
* dbSchema: import("../../../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined
* dbSchema: import("@/package-shared/types/database-schema.td").DSQL_DatabaseSchemaType | undefined
* }} params - function parameters inside an object
*
* @returns {Promise<FunctionReturn>} - Response object
*/
async function handleSocialDb({ social_id, email, social_platform, payload, res, supEmail, additionalFields, dbSchema }) {
const tableSchema = dbSchema?.tables.find((tb) => tb?.tableName === "users");
async function handleSocialDb({
social_id,
email,
social_platform,
payload,
res,
supEmail,
additionalFields,
dbSchema,
}) {
const tableSchema = dbSchema?.tables.find(
(tb) => tb?.tableName === "users"
);
try {
////////////////////////////////////////////////
@@ -244,7 +255,10 @@ async function handleSocialDb({ social_id, email, social_platform, payload, res,
////////////////////////////////////////////////
////////////////////////////////////////////////
} else {
console.log("Social User Failed to insert in 'handleSocialDb.js' backend function =>", newUser);
console.log(
"Social User Failed to insert in 'handleSocialDb.js' backend function =>",
newUser
);
return {
success: false,
@@ -258,7 +272,10 @@ async function handleSocialDb({ social_id, email, social_platform, payload, res,
////////////////////////////////////////////////
////////////////////////////////////////////////
} catch (/** @type {*} */ error) {
console.log("ERROR in 'handleSocialDb.js' backend function =>", error.message);
console.log(
"ERROR in 'handleSocialDb.js' backend function =>",
error.message
);
return {
success: false,
@@ -311,13 +328,22 @@ async function handleSocialDb({ social_id, email, social_platform, payload, res,
* msg?: string
* }>}
*/
async function loginSocialUser({ user, social_platform, res, database, additionalFields }) {
async function loginSocialUser({
user,
social_platform,
res,
database,
additionalFields,
}) {
const foundUser = await varDatabaseDbHandler({
database: database ? database : "datasquirel",
queryString: `SELECT * FROM users WHERE email='${user.email}' AND social_id='${user.social_id}' AND social_platform='${social_platform}'`,
});
let csrfKey = Math.random().toString(36).substring(2) + "-" + Math.random().toString(36).substring(2);
let csrfKey =
Math.random().toString(36).substring(2) +
"-" +
Math.random().toString(36).substring(2);
if (!foundUser?.[0]) {
return {
@@ -360,7 +386,10 @@ async function loginSocialUser({ user, social_platform, res, database, additiona
});
if (res?.setHeader) {
res.setHeader("Set-Cookie", [`datasquirelAuthKey=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `csrf=${csrfKey};samesite=strict;path=/;HttpOnly=true`]);
res.setHeader("Set-Cookie", [
`datasquirelAuthKey=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`,
`csrf=${csrfKey};samesite=strict;path=/;HttpOnly=true`,
]);
}
////////////////////////////////////////////////