This commit is contained in:
Benjamin Toby
2025-07-18 18:34:04 +01:00
parent a53b6e6974
commit 20a390e4a8
73 changed files with 1261 additions and 751 deletions
+11 -2
View File
@@ -9,6 +9,7 @@ import {
import grabHostNames from "../../utils/grab-host-names";
import serializeQuery from "../../utils/serialize-query";
import { RequestOptions } from "https";
import _ from "lodash";
type Param<T = { [k: string]: any }> = {
body?: T;
@@ -115,7 +116,12 @@ export default async function queryDSQLAPI<
payload: undefined,
msg: `An error occurred while parsing the response`,
error: error.message,
errorData: { requestOptions, grabedHostNames },
errorData: {
requestOptions,
grabedHostNames: _.omit(grabedHostNames, [
"scheme",
]),
},
} as APIResponseObject);
}
});
@@ -138,7 +144,10 @@ export default async function queryDSQLAPI<
payload: undefined,
msg: `An error occurred while making the request`,
error: err.message,
errorData: { requestOptions, grabedHostNames },
errorData: {
requestOptions,
grabedHostNames: _.omit(grabedHostNames, ["scheme"]),
},
} as APIResponseObject);
});
@@ -7,7 +7,7 @@ import encrypt from "../../dsql/encrypt";
import addDbEntry from "../../backend/db/addDbEntry";
import loginSocialUser from "./loginSocialUser";
import {
APILoginFunctionReturn,
APIResponseObject,
HandleSocialDbFunctionParams,
} from "../../../types";
import grabDirNames from "../../../utils/backend/names/grab-dir-names";
@@ -27,7 +27,7 @@ export default async function handleSocialDb({
debug,
loginOnly,
apiUserId,
}: HandleSocialDbFunctionParams): Promise<APILoginFunctionReturn> {
}: HandleSocialDbFunctionParams): Promise<APIResponseObject> {
try {
const finalDbName = grabDbFullName({
dbName: database,
@@ -200,16 +200,9 @@ export default async function handleSocialDb({
}).then(() => {});
}
const { STATIC_ROOT } = grabDirNames();
if (!STATIC_ROOT) {
console.log("Static File ENV not Found!");
return {
success: false,
payload: null,
msg: "Static File ENV not Found!",
};
}
const { userPrivateMediaDir, userPublicMediaDir } = grabDirNames({
userId: newUser.payload.insertId,
});
/**
* Create new user folder and file
@@ -217,21 +210,10 @@ export default async function handleSocialDb({
* @description Create new user folder and file
*/
if (!database || database?.match(/^datasquirel$/)) {
let newUserSchemaFolderPath = `${process.env.DSQL_USER_DB_SCHEMA_PATH}/user-${newUser.payload.insertId}`;
let newUserMediaFolderPath = path.join(
STATIC_ROOT,
`images/user-images/user-${newUser.payload.insertId}`
);
fs.mkdirSync(newUserSchemaFolderPath);
fs.mkdirSync(newUserMediaFolderPath);
fs.writeFileSync(
`${newUserSchemaFolderPath}/main.json`,
JSON.stringify([]),
"utf8"
);
userPublicMediaDir &&
fs.mkdirSync(userPublicMediaDir, { recursive: true });
userPrivateMediaDir &&
fs.mkdirSync(userPrivateMediaDir, { recursive: true });
}
return await loginSocialUser({
@@ -1,16 +1,9 @@
import addAdminUserOnLogin from "../../backend/addAdminUserOnLogin";
import dbHandler from "../../backend/dbHandler";
import {
APILoginFunctionReturn,
DATASQUIREL_LoggedInUser,
} from "../../../types";
import { APIResponseObject } from "../../../types";
import loginUser from "../../../actions/users/login-user";
type Param = {
user: {
first_name: string;
last_name: string;
email: string;
social_id: string | number;
};
social_platform: string;
invitation?: any;
@@ -32,68 +25,18 @@ export default async function loginSocialUser({
database,
additionalFields,
debug,
}: Param): Promise<APILoginFunctionReturn> {
}: Param): Promise<APIResponseObject> {
const finalDbName = database ? database : "datasquirel";
const dbAppend = database ? `\`${finalDbName}\`.` : "";
const foundUserQuery = `SELECT * FROM ${dbAppend}\`users\` WHERE email=?`;
const foundUserValues = [user.email];
const foundUser = (await dbHandler({
query: foundUserQuery,
values: foundUserValues,
let userPayload = await loginUser({
database: finalDbName,
})) as any[];
payload: { email: user.email },
skipPassword: true,
skipWriteAuthFile: true,
additionalFields,
debug,
useLocal: true,
});
if (!foundUser?.[0])
return {
success: false,
payload: null,
msg: "Couldn't find Social User.",
};
let csrfKey =
Math.random().toString(36).substring(2) +
"-" +
Math.random().toString(36).substring(2);
let userPayload: DATASQUIREL_LoggedInUser = {
id: foundUser[0].id,
uuid: foundUser[0].uuid,
first_name: foundUser[0].first_name,
last_name: foundUser[0].last_name,
username: foundUser[0].username,
user_type: foundUser[0].user_type,
email: foundUser[0].email,
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,
logged_in_status: true,
date: Date.now(),
};
if (additionalFields?.[0]) {
additionalFields.forEach((key) => {
userPayload[key] = foundUser[0][key];
});
}
if (invitation && (!database || database?.match(/^datasquirel$/))) {
addAdminUserOnLogin({
query: invitation,
user: userPayload,
});
}
let result: APILoginFunctionReturn = {
success: true,
payload: userPayload,
csrf: csrfKey,
};
return result;
return userPayload;
}
@@ -3,9 +3,9 @@ import handleSocialDb from "../../social-login/handleSocialDb";
import EJSON from "../../../../utils/ejson";
import {
APIGoogleLoginFunctionParams,
APILoginFunctionReturn,
GoogleOauth2User,
} from "../../../../types";
import { APIResponseObject } from "../../../../types";
/**
* # API google login
@@ -18,7 +18,7 @@ export default async function apiGoogleLogin({
debug,
loginOnly,
apiUserId,
}: APIGoogleLoginFunctionParams): Promise<APILoginFunctionReturn> {
}: APIGoogleLoginFunctionParams): Promise<APIResponseObject> {
try {
const gUser: GoogleOauth2User | undefined = await new Promise(
(resolve, reject) => {