This commit is contained in:
Benjamin Toby
2025-07-07 09:14:37 +01:00
parent 6acf0f2cd6
commit ccb5605b58
30 changed files with 292 additions and 166 deletions
+1 -1
View File
@@ -2,4 +2,4 @@ import { APILoginFunctionReturn, LoginUserParam } from "../../types";
/**
* # 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, apiUserID, skipWriteAuthFile, dbUserId, debug, cleanupTokens, secureCookie, request, }: LoginUserParam): 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, apiUserID, skipWriteAuthFile, dbUserId, debug, cleanupTokens, secureCookie, request, useLocal, }: LoginUserParam): Promise<APILoginFunctionReturn>;
+2 -15
View File
@@ -13,8 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = loginUser;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const encrypt_1 = __importDefault(require("../../functions/dsql/encrypt"));
const grab_host_names_1 = __importDefault(require("../../utils/grab-host-names"));
const api_login_1 = __importDefault(require("../../functions/api/users/api-login"));
@@ -26,7 +24,7 @@ const grab_cookie_expirt_date_1 = __importDefault(require("../../utils/grab-cook
* # Login A user
*/
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, apiUserID, skipWriteAuthFile, dbUserId, debug, cleanupTokens, secureCookie, request, }) {
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, apiUserID, skipWriteAuthFile, dbUserId, debug, cleanupTokens, secureCookie, request, useLocal, }) {
var _b, _c, _d;
const grabedHostNames = (0, grab_host_names_1.default)({ userId: user_id || apiUserID });
const { host, port, scheme } = grabedHostNames;
@@ -82,18 +80,7 @@ function loginUser(_a) {
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } = process.env;
if ((DSQL_DB_HOST === null || DSQL_DB_HOST === void 0 ? void 0 : DSQL_DB_HOST.match(/./)) &&
(DSQL_DB_USERNAME === null || DSQL_DB_USERNAME === void 0 ? void 0 : DSQL_DB_USERNAME.match(/./)) &&
(DSQL_DB_PASSWORD === null || DSQL_DB_PASSWORD === void 0 ? void 0 : DSQL_DB_PASSWORD.match(/./)) &&
(DSQL_DB_NAME === null || DSQL_DB_NAME === void 0 ? void 0 : DSQL_DB_NAME.match(/./)) &&
global.DSQL_USE_LOCAL) {
let dbSchema;
try {
const localDbSchemaPath = path_1.default.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs_1.default.readFileSync(localDbSchemaPath, "utf8"));
}
catch (error) { }
if (useLocal) {
httpResponse = yield (0, api_login_1.default)({
database: database || process.env.DSQL_DB_NAME || "",
email: payload.email,
+2 -1
View File
@@ -2,7 +2,7 @@ import http from "http";
import { SendOneTimeCodeEmailResponse } from "../../types";
type Param = {
key?: string;
database?: string;
database: string;
email: string;
temp_code_field_name?: string;
response?: http.ServerResponse & {
@@ -15,6 +15,7 @@ type Param = {
sender?: string;
user_id?: boolean;
extraCookies?: import("../../types").CookieObject[];
useLocal?: boolean;
};
/**
* # Send Email Code to a User
+4 -22
View File
@@ -13,8 +13,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = sendEmailCode;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const grab_host_names_1 = __importDefault(require("../../utils/grab-host-names"));
const api_send_email_code_1 = __importDefault(require("../../functions/api/users/api-send-email-code"));
/**
@@ -22,7 +20,7 @@ const api_send_email_code_1 = __importDefault(require("../../functions/api/users
*/
function sendEmailCode(params) {
return __awaiter(this, void 0, void 0, function* () {
const { key, email, database, temp_code_field_name, mail_domain, mail_password, mail_username, mail_port, sender, user_id, response, extraCookies, } = params;
const { key, email, database, temp_code_field_name, mail_domain, mail_password, mail_username, mail_port, sender, user_id, response, extraCookies, useLocal, } = params;
const grabedHostNames = (0, grab_host_names_1.default)();
const { host, port, scheme } = grabedHostNames;
const defaultTempLoginFieldName = "temp_login_code";
@@ -30,26 +28,10 @@ function sendEmailCode(params) {
? temp_code_field_name
: defaultTempLoginFieldName;
const emailHtml = `<p>Please use this code to login</p>\n<h2>{{code}}</h2>\n<p>Please note that this code expires after 15 minutes</p>`;
/**
* Check for local DB settings
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } = process.env;
if ((DSQL_DB_HOST === null || DSQL_DB_HOST === void 0 ? void 0 : DSQL_DB_HOST.match(/./)) &&
(DSQL_DB_USERNAME === null || DSQL_DB_USERNAME === void 0 ? void 0 : DSQL_DB_USERNAME.match(/./)) &&
(DSQL_DB_PASSWORD === null || DSQL_DB_PASSWORD === void 0 ? void 0 : DSQL_DB_PASSWORD.match(/./)) &&
(DSQL_DB_NAME === null || DSQL_DB_NAME === void 0 ? void 0 : DSQL_DB_NAME.match(/./)) &&
global.DSQL_USE_LOCAL) {
/** @type {import("../../types").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
try {
const localDbSchemaPath = path_1.default.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs_1.default.readFileSync(localDbSchemaPath, "utf8"));
}
catch (error) { }
console.log("useLocal", useLocal);
if (useLocal) {
return yield (0, api_send_email_code_1.default)({
database: DSQL_DB_NAME,
database,
email,
email_login_field: emailLoginTempCodeFieldName,
html: emailHtml,
+5 -1
View File
@@ -15,9 +15,13 @@ type Param = {
debug?: boolean;
secureCookie?: boolean;
loginOnly?: boolean;
/**
* Login without calling external API
*/
forceLocal?: boolean;
};
/**
* # SERVER FUNCTION: Login with google Function
*/
export default function googleAuth({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, additionalData, apiUserID, debug, secureCookie, loginOnly, }: Param): Promise<APILoginFunctionReturn>;
export default function googleAuth({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, additionalData, apiUserID, debug, secureCookie, loginOnly, forceLocal, }: Param): Promise<APILoginFunctionReturn>;
export {};
+3 -12
View File
@@ -23,7 +23,7 @@ const grab_cookie_expirt_date_1 = __importDefault(require("../../../utils/grab-c
* # SERVER FUNCTION: Login with google Function
*/
function googleAuth(_a) {
return __awaiter(this, arguments, void 0, function* ({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, additionalData, apiUserID, debug, secureCookie, loginOnly, }) {
return __awaiter(this, arguments, void 0, function* ({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, additionalData, apiUserID, debug, secureCookie, loginOnly, forceLocal, }) {
var _b;
const grabedHostNames = (0, grab_host_names_1.default)({
userId: apiUserID || process.env.DSQL_API_USER_ID,
@@ -66,17 +66,7 @@ function googleAuth(_a) {
let httpResponse = {
success: false,
};
/**
* Check for local DB settings
*
* @description Look for local db settings in `.env` file and by pass the http request if available
*/
const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } = process.env;
if ((DSQL_DB_HOST === null || DSQL_DB_HOST === void 0 ? void 0 : DSQL_DB_HOST.match(/./)) &&
(DSQL_DB_USERNAME === null || DSQL_DB_USERNAME === void 0 ? void 0 : DSQL_DB_USERNAME.match(/./)) &&
(DSQL_DB_PASSWORD === null || DSQL_DB_PASSWORD === void 0 ? void 0 : DSQL_DB_PASSWORD.match(/./)) &&
(DSQL_DB_NAME === null || DSQL_DB_NAME === void 0 ? void 0 : DSQL_DB_NAME.match(/./)) &&
global.DSQL_USE_LOCAL) {
if (forceLocal) {
if (debug) {
console.log(`Google login with Local Paradigm ...`);
}
@@ -85,6 +75,7 @@ function googleAuth(_a) {
additionalFields,
additionalData,
debug,
loginOnly,
});
}
else {