Updates
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { findDbNameInSchemaDir } from "../../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
import { grabPrimaryRequiredDbSchema } from "../../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
import { APICreateUserFunctionParams } from "../../../types";
|
||||
import addUsersTableToDb from "../../backend/addUsersTableToDb";
|
||||
import addDbEntry from "../../backend/db/addDbEntry";
|
||||
import updateUsersTableSchema from "../../backend/updateUsersTableSchema";
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import hashPassword from "../../dsql/hashPassword";
|
||||
import validateEmail from "../../email/fns/validate-email";
|
||||
import { DSQL_DATASQUIREL_USERS } from "@/package-shared/types/dsql";
|
||||
|
||||
/**
|
||||
* # API Create User
|
||||
@@ -15,10 +16,8 @@ export default async function apiCreateUser({
|
||||
payload,
|
||||
database,
|
||||
userId,
|
||||
verify,
|
||||
}: APICreateUserFunctionParams) {
|
||||
const dbFullName = database;
|
||||
const API_USER_ID = userId || process.env.DSQL_API_USER_ID;
|
||||
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
|
||||
@@ -38,9 +37,10 @@ export default async function apiCreateUser({
|
||||
};
|
||||
}
|
||||
|
||||
const targetDbSchema = findDbNameInSchemaDir({
|
||||
dbName: dbFullName,
|
||||
const targetDbSchema = grabPrimaryRequiredDbSchema({
|
||||
dbSlug: database,
|
||||
userId,
|
||||
dbId: userId ? undefined : 1,
|
||||
});
|
||||
|
||||
if (!targetDbSchema?.id) {
|
||||
@@ -51,6 +51,16 @@ export default async function apiCreateUser({
|
||||
};
|
||||
}
|
||||
|
||||
const dbFullName = targetDbSchema.dbFullName;
|
||||
|
||||
if (!dbFullName) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "dbFullName not found",
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
|
||||
const hashedPassword = hashPassword({
|
||||
encryptionKey: finalEncryptionKey,
|
||||
password: String(payload.password),
|
||||
@@ -60,23 +70,23 @@ export default async function apiCreateUser({
|
||||
|
||||
const fieldsQuery = `SHOW COLUMNS FROM ${dbFullName}.users`;
|
||||
|
||||
let fields = await varDatabaseDbHandler({
|
||||
queryString: fieldsQuery,
|
||||
let fields = (await dbHandler({
|
||||
query: fieldsQuery,
|
||||
database: dbFullName,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
if (!fields?.[0]) {
|
||||
const newTable = await addUsersTableToDb({
|
||||
userId: Number(API_USER_ID),
|
||||
userId,
|
||||
database: dbFullName,
|
||||
payload: payload,
|
||||
dbId: targetDbSchema.id,
|
||||
});
|
||||
|
||||
fields = await varDatabaseDbHandler({
|
||||
queryString: fieldsQuery,
|
||||
fields = (await dbHandler({
|
||||
query: fieldsQuery,
|
||||
database: dbFullName,
|
||||
});
|
||||
})) as any[];
|
||||
}
|
||||
|
||||
if (!fields?.[0]) {
|
||||
@@ -94,7 +104,7 @@ export default async function apiCreateUser({
|
||||
const key = Object.keys(payload)[i];
|
||||
if (!fieldsTitles.includes(key)) {
|
||||
await updateUsersTableSchema({
|
||||
userId: Number(API_USER_ID),
|
||||
userId,
|
||||
database: dbFullName,
|
||||
newPayload: {
|
||||
[key]: payload[key],
|
||||
@@ -118,11 +128,11 @@ export default async function apiCreateUser({
|
||||
? [payload.email, payload.username]
|
||||
: [payload.email];
|
||||
|
||||
const existingUser = await varDatabaseDbHandler({
|
||||
queryString: existingUserQuery,
|
||||
queryValuesArray: existingUserValues,
|
||||
const existingUser = (await dbHandler({
|
||||
query: existingUserQuery,
|
||||
values: existingUserValues,
|
||||
database: dbFullName,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
if (existingUser?.[0]) {
|
||||
return {
|
||||
@@ -142,7 +152,7 @@ export default async function apiCreateUser({
|
||||
};
|
||||
}
|
||||
|
||||
const addUser = await addDbEntry({
|
||||
const addUser = await addDbEntry<DSQL_DATASQUIREL_USERS>({
|
||||
dbFullName: dbFullName,
|
||||
tableName: "users",
|
||||
data: {
|
||||
@@ -153,16 +163,18 @@ export default async function apiCreateUser({
|
||||
image_thumbnail:
|
||||
process.env.DSQL_DEFAULT_USER_IMAGE ||
|
||||
"/images/user-preset-thumbnail.png",
|
||||
verification_status: verify ? 1 : 0,
|
||||
},
|
||||
});
|
||||
|
||||
if (addUser?.payload?.insertId) {
|
||||
const newlyAddedUserQuery = `SELECT id,uuid,first_name,last_name,email,username,image,image_thumbnail,verification_status FROM ${dbFullName}.users WHERE id='${addUser.payload.insertId}'`;
|
||||
|
||||
const newlyAddedUser = await varDatabaseDbHandler({
|
||||
queryString: newlyAddedUserQuery,
|
||||
const newlyAddedUser = (await dbHandler({
|
||||
query: newlyAddedUserQuery,
|
||||
values: [],
|
||||
database: dbFullName,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
return {
|
||||
success: true,
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import deleteDbEntry from "../../backend/db/deleteDbEntry";
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
dbFullName: string;
|
||||
@@ -17,11 +17,11 @@ export default async function apiDeleteUser({
|
||||
const existingUserQuery = `SELECT * FROM ${dbFullName}.users WHERE id = ?`;
|
||||
const existingUserValues = [deletedUserId];
|
||||
|
||||
const existingUser = await varDatabaseDbHandler({
|
||||
queryString: existingUserQuery,
|
||||
queryValuesArray: existingUserValues,
|
||||
const existingUser = (await dbHandler({
|
||||
query: existingUserQuery,
|
||||
values: existingUserValues,
|
||||
database: dbFullName,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
if (!existingUser?.[0]) {
|
||||
return {
|
||||
|
||||
@@ -2,7 +2,7 @@ import {
|
||||
APIGetUserFunctionParams,
|
||||
GetUserFunctionReturn,
|
||||
} from "../../../types";
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
|
||||
/**
|
||||
* # API Get User
|
||||
@@ -19,11 +19,11 @@ export default async function apiGetUser({
|
||||
)} FROM ${finalDbName}.users WHERE id=?`;
|
||||
const API_USER_ID = userId || process.env.DSQL_API_USER_ID;
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: query,
|
||||
queryValuesArray: [API_USER_ID],
|
||||
let foundUser = (await dbHandler({
|
||||
query,
|
||||
values: [API_USER_ID],
|
||||
database: finalDbName,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
if (!foundUser || !foundUser[0]) {
|
||||
return {
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
DATASQUIREL_LoggedInUser,
|
||||
} from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import hashPassword from "../../dsql/hashPassword";
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,6 @@ export default async function apiLoginUser({
|
||||
msg: `Database Full Name couldn't be grabbed`,
|
||||
};
|
||||
}
|
||||
const dbAppend = global.DSQL_USE_LOCAL ? "" : `${dbFullName}.`;
|
||||
|
||||
/**
|
||||
* Check input validity
|
||||
@@ -69,13 +68,11 @@ export default async function apiLoginUser({
|
||||
console.log("apiLoginUser:Finding User ...");
|
||||
}
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM ${dbAppend}users WHERE email = ? OR username = ?`,
|
||||
queryValuesArray: [email, username],
|
||||
let foundUser = (await dbHandler({
|
||||
query: `SELECT * FROM users WHERE email = ? OR username = ?`,
|
||||
values: [email, username],
|
||||
database: dbFullName,
|
||||
|
||||
debug,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
if (debug) {
|
||||
console.log("apiLoginUser:foundUser:", foundUser);
|
||||
@@ -146,9 +143,9 @@ export default async function apiLoginUser({
|
||||
}
|
||||
|
||||
if (isPasswordCorrect && email_login) {
|
||||
const resetTempCode = await varDatabaseDbHandler({
|
||||
queryString: `UPDATE ${dbAppend}users SET ${email_login_field} = '' WHERE email = ? OR username = ?`,
|
||||
queryValuesArray: [email, username],
|
||||
await dbHandler({
|
||||
query: `UPDATE users SET ${email_login_field} = '' WHERE email = ? OR username = ?`,
|
||||
values: [email, username],
|
||||
database: dbFullName,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { APILoginFunctionReturn } from "../../../types";
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
existingUser: { [s: string]: any };
|
||||
@@ -15,19 +15,13 @@ export default async function apiReauthUser({
|
||||
database,
|
||||
additionalFields,
|
||||
}: Param): Promise<APILoginFunctionReturn> {
|
||||
const dbAppend = global.DSQL_USE_LOCAL
|
||||
? ""
|
||||
: database
|
||||
? `${database}.`
|
||||
: "";
|
||||
|
||||
let foundUser =
|
||||
existingUser?.id && existingUser.id.toString().match(/./)
|
||||
? await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM ${dbAppend}users WHERE id=?`,
|
||||
queryValuesArray: [existingUser.id.toString()],
|
||||
? ((await dbHandler({
|
||||
query: `SELECT * FROM users WHERE id=?`,
|
||||
values: [existingUser.id.toString()],
|
||||
database,
|
||||
})
|
||||
})) as any[])
|
||||
: null;
|
||||
|
||||
if (!foundUser || !foundUser[0])
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import nodemailer, { SendMailOptions } from "nodemailer";
|
||||
import http from "http";
|
||||
import getAuthCookieNames from "../../backend/cookies/get-auth-cookie-names";
|
||||
import encrypt from "../../dsql/encrypt";
|
||||
import serializeCookies from "../../../utils/serialize-cookies";
|
||||
import { SendOneTimeCodeEmailResponse } from "../../../types";
|
||||
import { CookieObject, SendOneTimeCodeEmailResponse } from "../../../types";
|
||||
|
||||
type Param = {
|
||||
email: string;
|
||||
@@ -17,7 +17,7 @@ type Param = {
|
||||
mail_password?: string;
|
||||
html: string;
|
||||
response?: http.ServerResponse & { [s: string]: any };
|
||||
extraCookies?: import("../../../../package-shared/types").CookieObject[];
|
||||
extraCookies?: CookieObject[];
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -47,15 +47,11 @@ export default async function apiSendEmailCode({
|
||||
const foundUserQuery = `SELECT * FROM ${database}.users WHERE email = ?`;
|
||||
const foundUserValues = [email];
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: foundUserQuery,
|
||||
queryValuesArray: foundUserValues,
|
||||
let foundUser = (await dbHandler({
|
||||
query: foundUserQuery,
|
||||
values: foundUserValues,
|
||||
database,
|
||||
});
|
||||
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
////////////////////////////////////////
|
||||
})) as any[];
|
||||
|
||||
if (!foundUser || !foundUser[0]) {
|
||||
return {
|
||||
@@ -107,13 +103,12 @@ export default async function apiSendEmailCode({
|
||||
const setTempCodeQuery = `UPDATE ${database}.users SET ${email_login_field} = ? WHERE email = ?`;
|
||||
const setTempCodeValues = [tempCode + `-${createdAt}`, email];
|
||||
|
||||
let setTempCode = await varDatabaseDbHandler({
|
||||
queryString: setTempCodeQuery,
|
||||
queryValuesArray: setTempCodeValues,
|
||||
await dbHandler({
|
||||
query: setTempCodeQuery,
|
||||
values: setTempCodeValues,
|
||||
database,
|
||||
});
|
||||
|
||||
/** @type {import("../../../types").SendOneTimeCodeEmailResponse} */
|
||||
const resObject: import("../../../types").SendOneTimeCodeEmailResponse =
|
||||
{
|
||||
success: true,
|
||||
@@ -137,22 +132,18 @@ export default async function apiSendEmailCode({
|
||||
);
|
||||
}
|
||||
|
||||
/** @type {import("../../../../package-shared/types").CookieObject} */
|
||||
const oneTimeCookieObject: import("../../../../package-shared/types").CookieObject =
|
||||
{
|
||||
name: oneTimeCodeCookieName,
|
||||
value: encryptedPayload,
|
||||
sameSite: "Strict",
|
||||
path: "/",
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
};
|
||||
const oneTimeCookieObject: CookieObject = {
|
||||
name: oneTimeCodeCookieName,
|
||||
value: encryptedPayload,
|
||||
sameSite: "Strict",
|
||||
path: "/",
|
||||
httpOnly: true,
|
||||
secure: true,
|
||||
};
|
||||
|
||||
/** @type {import("../../../../package-shared/types").CookieObject[]} */
|
||||
const cookiesObjectArray: import("../../../../package-shared/types").CookieObject[] =
|
||||
extraCookies
|
||||
? [...extraCookies, oneTimeCookieObject]
|
||||
: [oneTimeCookieObject];
|
||||
const cookiesObjectArray: CookieObject[] = extraCookies
|
||||
? [...extraCookies, oneTimeCookieObject]
|
||||
: [oneTimeCookieObject];
|
||||
|
||||
const serializedCookies = serializeCookies({
|
||||
cookies: cookiesObjectArray,
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
import updateDbEntry from "../../backend/db/updateDbEntry";
|
||||
import encrypt from "../../dsql/encrypt";
|
||||
import hashPassword from "../../dsql/hashPassword";
|
||||
import varDatabaseDbHandler from "../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
payload: { [s: string]: any };
|
||||
@@ -26,11 +26,11 @@ export default async function apiUpdateUser({
|
||||
const existingUserQuery = `SELECT * FROM ${dbFullName}.users WHERE id = ?`;
|
||||
const existingUserValues = [updatedUserId];
|
||||
|
||||
const existingUser = await varDatabaseDbHandler({
|
||||
queryString: existingUserQuery,
|
||||
queryValuesArray: existingUserValues,
|
||||
const existingUser = (await dbHandler({
|
||||
query: existingUserQuery,
|
||||
values: existingUserValues,
|
||||
database: dbFullName,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
if (!existingUser?.[0]) {
|
||||
return {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { DSQL_MYSQL_user_databases_Type } from "../../../../types";
|
||||
import grabDbFullName from "../../../../utils/grab-db-full-name";
|
||||
import varDatabaseDbHandler from "../../../backend/varDatabaseDbHandler";
|
||||
import dbHandler from "../../../backend/dbHandler";
|
||||
|
||||
type Return = {
|
||||
success: boolean;
|
||||
@@ -44,12 +44,11 @@ export default async function apiSendResetPasswordLink({
|
||||
};
|
||||
}
|
||||
|
||||
let foundUser = await varDatabaseDbHandler({
|
||||
queryString: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
|
||||
queryValuesArray: [email, email],
|
||||
let foundUser = (await dbHandler({
|
||||
query: `SELECT * FROM ${dbFullName}.users WHERE email = ? OR username = ?`,
|
||||
values: [email, email],
|
||||
database: dbFullName,
|
||||
debug,
|
||||
});
|
||||
})) as any[];
|
||||
|
||||
if (debug) {
|
||||
console.log("apiSendResetPassword:foundUser:", foundUser);
|
||||
|
||||
Reference in New Issue
Block a user