This commit is contained in:
Benjamin Toby 2025-01-14 11:33:30 +01:00
parent d55fe64d47
commit 03d4fdc270
12 changed files with 79 additions and 52 deletions

View File

@ -2,4 +2,4 @@ import { APILoginFunctionParams, APILoginFunctionReturn } from "../../../types";
/** /**
* # API Login * # API Login
*/ */
export default function apiLoginUser({ encryptionKey, email, username, password, database, additionalFields, email_login, email_login_code, email_login_field, token, skipPassword, social, useLocal, }: APILoginFunctionParams): Promise<APILoginFunctionReturn>; export default function apiLoginUser({ encryptionKey, email, username, password, database, additionalFields, email_login, email_login_code, email_login_field, token, skipPassword, social, useLocal, dbUserId, }: APILoginFunctionParams): Promise<APILoginFunctionReturn>;

View File

@ -13,14 +13,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.default = apiLoginUser; exports.default = apiLoginUser;
const grab_db_full_name_1 = __importDefault(require("../../../utils/grab-db-full-name"));
const varDatabaseDbHandler_1 = __importDefault(require("../../backend/varDatabaseDbHandler")); const varDatabaseDbHandler_1 = __importDefault(require("../../backend/varDatabaseDbHandler"));
const hashPassword_1 = __importDefault(require("../../dsql/hashPassword")); const hashPassword_1 = __importDefault(require("../../dsql/hashPassword"));
/** /**
* # API Login * # API Login
*/ */
function apiLoginUser(_a) { function apiLoginUser(_a) {
return __awaiter(this, arguments, void 0, function* ({ encryptionKey, email, username, password, database, additionalFields, email_login, email_login_code, email_login_field, token, skipPassword, social, useLocal, }) { return __awaiter(this, arguments, void 0, function* ({ encryptionKey, email, username, password, database, additionalFields, email_login, email_login_code, email_login_field, token, skipPassword, social, useLocal, dbUserId, }) {
const dbFullName = database.replace(/[^a-z0-9_]/g, ""); const dbFullName = (0, grab_db_full_name_1.default)({ dbName: database, userId: dbUserId });
/** /**
* Check input validity * Check input validity
* *
@ -45,14 +46,13 @@ function apiLoginUser(_a) {
password: password, password: password,
}) })
: null; : null;
console.log("Finding DSQL User ..."); console.log(`Logging in: Checking for Existing user in ${dbFullName} database.`);
let foundUser = yield (0, varDatabaseDbHandler_1.default)({ let foundUser = yield (0, varDatabaseDbHandler_1.default)({
queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`, queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
queryValuesArray: [email, username], queryValuesArray: [email, username],
database: dbFullName, database: dbFullName,
useLocal, useLocal,
}); });
console.log("foundUser", foundUser);
if ((!foundUser || !foundUser[0]) && !social) if ((!foundUser || !foundUser[0]) && !social)
return { return {
success: false, success: false,

View File

@ -143,6 +143,7 @@ export interface PackageUserLoginRequestBody {
social?: boolean; social?: boolean;
dbSchema?: DSQL_DatabaseSchemaType; dbSchema?: DSQL_DatabaseSchemaType;
skipPassword?: boolean; skipPassword?: boolean;
dbUserId: string | number;
} }
export interface PackageUserLoginLocalBody { export interface PackageUserLoginLocalBody {
payload: any; payload: any;
@ -1026,6 +1027,7 @@ export type APILoginFunctionParams = {
skipPassword?: boolean; skipPassword?: boolean;
social?: boolean; social?: boolean;
useLocal?: boolean; useLocal?: boolean;
dbUserId?: number | string;
}; };
export type APILoginFunctionReturn = { export type APILoginFunctionReturn = {
success: boolean; success: boolean;

View File

@ -0,0 +1,9 @@
type Param = {
dbName: string;
userId?: string | number;
};
/**
* # Grab Database Full Name
*/
export default function grabDbFullName({ dbName, userId }: Param): string;
export {};

View File

@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = grabDbFullName;
/**
* # Grab Database Full Name
*/
function grabDbFullName({ dbName, userId }) {
const sanitizedName = dbName.replace(/[^a-z0-9\_]/g, "");
const cleanedDbName = sanitizedName.replace(/datasquirel_user_\d+_/, "");
if (!userId)
return cleanedDbName;
const dbNamePrefix = `datasquirel_user_${userId}_`;
return dbNamePrefix + cleanedDbName;
}

View File

@ -23,9 +23,10 @@ type Param = {
useLocal?: boolean; useLocal?: boolean;
skipWriteAuthFile?: boolean; skipWriteAuthFile?: boolean;
apiUserID?: string | number; apiUserID?: string | number;
dbUserId?: string | number;
}; };
/** /**
* # Login A user * # Login A user
*/ */
export default function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, useLocal, apiUserID, skipWriteAuthFile, }: Param): Promise<APILoginFunctionReturn>; export default function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, useLocal, apiUserID, skipWriteAuthFile, dbUserId, }: Param): Promise<APILoginFunctionReturn>;
export {}; export {};

View File

@ -24,7 +24,7 @@ const write_auth_files_1 = require("../package-shared/functions/backend/auth/wri
* # Login A user * # Login A user
*/ */
function loginUser(_a) { function loginUser(_a) {
return __awaiter(this, arguments, void 0, function* ({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, useLocal, apiUserID, skipWriteAuthFile, }) { return __awaiter(this, arguments, void 0, function* ({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, useLocal, apiUserID, skipWriteAuthFile, dbUserId, }) {
var _b; var _b;
const grabedHostNames = (0, grab_host_names_1.default)(); const grabedHostNames = (0, grab_host_names_1.default)();
const { host, port, scheme } = grabedHostNames; const { host, port, scheme } = grabedHostNames;
@ -101,18 +101,11 @@ function loginUser(_a) {
email_login_field: emailLoginTempCodeFieldName, email_login_field: emailLoginTempCodeFieldName,
token, token,
useLocal, useLocal,
dbUserId,
}); });
} }
else { else {
/**
* Make https request
*
* @description make a request to datasquirel.com
*
* @type {{ success: boolean, payload: import("../package-shared/types").DATASQUIREL_LoggedInUser | null, userId?: number, msg?: string }}
*/
httpResponse = yield new Promise((resolve, reject) => { httpResponse = yield new Promise((resolve, reject) => {
/** @type {import("../package-shared/types").PackageUserLoginRequestBody} */
const reqPayload = { const reqPayload = {
encryptionKey: finalEncryptionKey, encryptionKey: finalEncryptionKey,
payload, payload,
@ -123,6 +116,7 @@ function loginUser(_a) {
email_login_field: emailLoginTempCodeFieldName, email_login_field: emailLoginTempCodeFieldName,
token, token,
skipPassword: skipPassword, skipPassword: skipPassword,
dbUserId: dbUserId || 0,
}; };
const reqPayloadJSON = JSON.stringify(reqPayload); const reqPayloadJSON = JSON.stringify(reqPayload);
const httpsRequest = scheme.request({ const httpsRequest = scheme.request({
@ -137,13 +131,7 @@ function loginUser(_a) {
port, port,
hostname: host, hostname: host,
path: `/api/user/${user_id || grabedHostNames.user_id}/login-user`, path: `/api/user/${user_id || grabedHostNames.user_id}/login-user`,
}, }, (res) => {
/**
* Callback Function
*
* @description https request callback
*/
(res) => {
var str = ""; var str = "";
res.on("data", function (chunk) { res.on("data", function (chunk) {
str += chunk; str += chunk;

View File

@ -3,6 +3,7 @@ import {
APILoginFunctionReturn, APILoginFunctionReturn,
DATASQUIREL_LoggedInUser, DATASQUIREL_LoggedInUser,
} from "../../../types"; } from "../../../types";
import grabDbFullName from "../../../utils/grab-db-full-name";
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler"; import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
import hashPassword from "../../dsql/hashPassword"; import hashPassword from "../../dsql/hashPassword";
@ -23,8 +24,9 @@ export default async function apiLoginUser({
skipPassword, skipPassword,
social, social,
useLocal, useLocal,
dbUserId,
}: APILoginFunctionParams): Promise<APILoginFunctionReturn> { }: APILoginFunctionParams): Promise<APILoginFunctionReturn> {
const dbFullName = database.replace(/[^a-z0-9_]/g, ""); const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
/** /**
* Check input validity * Check input validity
@ -54,7 +56,9 @@ export default async function apiLoginUser({
}) })
: null; : null;
console.log("Finding DSQL User ..."); console.log(
`Logging in: Checking for Existing user in ${dbFullName} database.`
);
let foundUser = await varDatabaseDbHandler({ let foundUser = await varDatabaseDbHandler({
queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`, queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
@ -63,8 +67,6 @@ export default async function apiLoginUser({
useLocal, useLocal,
}); });
console.log("foundUser", foundUser);
if ((!foundUser || !foundUser[0]) && !social) if ((!foundUser || !foundUser[0]) && !social)
return { return {
success: false, success: false,

View File

@ -158,6 +158,7 @@ export interface PackageUserLoginRequestBody {
social?: boolean; social?: boolean;
dbSchema?: DSQL_DatabaseSchemaType; dbSchema?: DSQL_DatabaseSchemaType;
skipPassword?: boolean; skipPassword?: boolean;
dbUserId: string | number;
} }
export interface PackageUserLoginLocalBody { export interface PackageUserLoginLocalBody {
@ -1208,6 +1209,7 @@ export type APILoginFunctionParams = {
skipPassword?: boolean; skipPassword?: boolean;
social?: boolean; social?: boolean;
useLocal?: boolean; useLocal?: boolean;
dbUserId?: number | string;
}; };
export type APILoginFunctionReturn = { export type APILoginFunctionReturn = {
success: boolean; success: boolean;

View File

@ -0,0 +1,18 @@
type Param = {
dbName: string;
userId?: string | number;
};
/**
* # Grab Database Full Name
*/
export default function grabDbFullName({ dbName, userId }: Param): string {
const sanitizedName = dbName.replace(/[^a-z0-9\_]/g, "");
const cleanedDbName = sanitizedName.replace(/datasquirel_user_\d+_/, "");
if (!userId) return cleanedDbName;
const dbNamePrefix = `datasquirel_user_${userId}_`;
return dbNamePrefix + cleanedDbName;
}

View File

@ -1,6 +1,6 @@
{ {
"name": "@moduletrace/datasquirel", "name": "@moduletrace/datasquirel",
"version": "3.5.0", "version": "3.5.1",
"description": "Cloud-based SQL data management tool", "description": "Cloud-based SQL data management tool",
"main": "dist/index.js", "main": "dist/index.js",
"bin": { "bin": {

View File

@ -9,6 +9,7 @@ import { writeAuthFile } from "../package-shared/functions/backend/auth/write-au
import { import {
APILoginFunctionReturn, APILoginFunctionReturn,
DSQL_DatabaseSchemaType, DSQL_DatabaseSchemaType,
PackageUserLoginRequestBody,
} from "../package-shared/types"; } from "../package-shared/types";
type Param = { type Param = {
@ -32,6 +33,7 @@ type Param = {
useLocal?: boolean; useLocal?: boolean;
skipWriteAuthFile?: boolean; skipWriteAuthFile?: boolean;
apiUserID?: string | number; apiUserID?: string | number;
dbUserId?: string | number;
}; };
/** /**
@ -54,6 +56,7 @@ export default async function loginUser({
useLocal, useLocal,
apiUserID, apiUserID,
skipWriteAuthFile, skipWriteAuthFile,
dbUserId,
}: Param): Promise<APILoginFunctionReturn> { }: Param): Promise<APILoginFunctionReturn> {
const grabedHostNames = grabHostNames(); const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames; const { host, port, scheme } = grabedHostNames;
@ -148,19 +151,11 @@ export default async function loginUser({
email_login_field: emailLoginTempCodeFieldName, email_login_field: emailLoginTempCodeFieldName,
token, token,
useLocal, useLocal,
dbUserId,
}); });
} else { } else {
/**
* Make https request
*
* @description make a request to datasquirel.com
*
* @type {{ success: boolean, payload: import("../package-shared/types").DATASQUIREL_LoggedInUser | null, userId?: number, msg?: string }}
*/
httpResponse = await new Promise((resolve, reject) => { httpResponse = await new Promise((resolve, reject) => {
/** @type {import("../package-shared/types").PackageUserLoginRequestBody} */ const reqPayload: PackageUserLoginRequestBody = {
const reqPayload: import("../package-shared/types").PackageUserLoginRequestBody =
{
encryptionKey: finalEncryptionKey, encryptionKey: finalEncryptionKey,
payload, payload,
database, database,
@ -170,6 +165,7 @@ export default async function loginUser({
email_login_field: emailLoginTempCodeFieldName, email_login_field: emailLoginTempCodeFieldName,
token, token,
skipPassword: skipPassword, skipPassword: skipPassword,
dbUserId: dbUserId || 0,
}; };
const reqPayloadJSON = JSON.stringify(reqPayload); const reqPayloadJSON = JSON.stringify(reqPayload);
@ -192,11 +188,6 @@ export default async function loginUser({
}/login-user`, }/login-user`,
}, },
/**
* Callback Function
*
* @description https request callback
*/
(res) => { (res) => {
var str = ""; var str = "";