Updates
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { grabPrimaryRequiredDbSchema } from "../../../shell/createDbFromSchema/grab-required-database-schemas";
|
||||
import { APICreateUserFunctionParams } from "../../../types";
|
||||
import { APICreateUserFunctionParams, APIResponseObject } from "../../../types";
|
||||
import addUsersTableToDb from "../../backend/addUsersTableToDb";
|
||||
import addDbEntry from "../../backend/db/addDbEntry";
|
||||
import updateUsersTableSchema from "../../backend/updateUsersTableSchema";
|
||||
@@ -15,9 +15,9 @@ export default async function apiCreateUser({
|
||||
encryptionKey,
|
||||
payload,
|
||||
database,
|
||||
userId,
|
||||
dsqlUserID,
|
||||
verify,
|
||||
}: APICreateUserFunctionParams) {
|
||||
}: APICreateUserFunctionParams): Promise<APIResponseObject> {
|
||||
const finalEncryptionKey =
|
||||
encryptionKey || process.env.DSQL_ENCRYPTION_PASSWORD;
|
||||
|
||||
@@ -39,8 +39,8 @@ export default async function apiCreateUser({
|
||||
|
||||
const targetDbSchema = grabPrimaryRequiredDbSchema({
|
||||
dbSlug: database,
|
||||
userId,
|
||||
dbId: userId ? undefined : 1,
|
||||
userId: dsqlUserID,
|
||||
dbId: dsqlUserID ? undefined : 1,
|
||||
});
|
||||
|
||||
if (!targetDbSchema?.id) {
|
||||
@@ -77,23 +77,19 @@ export default async function apiCreateUser({
|
||||
|
||||
if (!fields?.[0]) {
|
||||
const newTable = await addUsersTableToDb({
|
||||
userId,
|
||||
userId: dsqlUserID,
|
||||
database: dbFullName,
|
||||
payload: payload,
|
||||
dbId: targetDbSchema.id,
|
||||
});
|
||||
|
||||
fields = (await dbHandler({
|
||||
query: fieldsQuery,
|
||||
database: dbFullName,
|
||||
})) as any[];
|
||||
}
|
||||
|
||||
if (!fields?.[0]) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Could not create users table",
|
||||
};
|
||||
if (!newTable) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Could not create users table",
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const fieldsTitles = fields.map((fieldObject: any) => fieldObject.Field);
|
||||
@@ -104,7 +100,7 @@ export default async function apiCreateUser({
|
||||
const key = Object.keys(payload)[i];
|
||||
if (!fieldsTitles.includes(key)) {
|
||||
await updateUsersTableSchema({
|
||||
userId,
|
||||
userId: dsqlUserID,
|
||||
database: dbFullName,
|
||||
newPayload: {
|
||||
[key]: payload[key],
|
||||
@@ -177,15 +173,13 @@ export default async function apiCreateUser({
|
||||
})) as any[];
|
||||
|
||||
return {
|
||||
success: true,
|
||||
...addUser,
|
||||
payload: newlyAddedUser[0],
|
||||
};
|
||||
} else {
|
||||
return {
|
||||
success: false,
|
||||
...addUser,
|
||||
msg: "Could not create user",
|
||||
sqlResult: addUser,
|
||||
payload: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import {
|
||||
APIGetUserFunctionParams,
|
||||
GetUserFunctionReturn,
|
||||
} from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
|
||||
/**
|
||||
@@ -9,19 +10,18 @@ import dbHandler from "../../backend/dbHandler";
|
||||
*/
|
||||
export default async function apiGetUser({
|
||||
fields,
|
||||
dbFullName,
|
||||
database,
|
||||
userId,
|
||||
dbUserId,
|
||||
selectAll,
|
||||
}: APIGetUserFunctionParams): Promise<GetUserFunctionReturn> {
|
||||
const finalDbName = dbFullName.replace(/[^a-z0-9_]/g, "");
|
||||
const finalDbName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
const query = `SELECT ${fields.join(
|
||||
","
|
||||
)} FROM ${finalDbName}.users WHERE id=?`;
|
||||
const API_USER_ID = userId || process.env.DSQL_API_USER_ID;
|
||||
const selectFields = selectAll ? "*" : fields?.[0] ? fields.join(",") : "*";
|
||||
|
||||
let foundUser = (await dbHandler({
|
||||
query,
|
||||
values: [API_USER_ID],
|
||||
query: `SELECT ${selectFields} FROM users WHERE id=?`,
|
||||
values: [userId],
|
||||
database: finalDbName,
|
||||
})) as any[];
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import {
|
||||
APILoginFunctionParams,
|
||||
APILoginFunctionReturn,
|
||||
APIResponseObject,
|
||||
DATASQUIREL_LoggedInUser,
|
||||
} from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
@@ -24,7 +24,7 @@ export default async function apiLoginUser({
|
||||
social,
|
||||
dbUserId,
|
||||
debug,
|
||||
}: APILoginFunctionParams): Promise<APILoginFunctionReturn> {
|
||||
}: APILoginFunctionParams): Promise<APIResponseObject> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
if (!dbFullName) {
|
||||
@@ -181,7 +181,7 @@ export default async function apiLoginUser({
|
||||
console.log("apiLoginUser:Sending Response Object ...");
|
||||
}
|
||||
|
||||
const resposeObject: APILoginFunctionReturn = {
|
||||
const resposeObject: APIResponseObject = {
|
||||
success: true,
|
||||
msg: "Login Successful",
|
||||
payload: userPayload,
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import updateDbEntry from "../../backend/db/updateDbEntry";
|
||||
import hashPassword from "../../dsql/hashPassword";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
import { APIResponseObject, ResetPasswordParams } from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
import { DSQL_DATASQUIREL_USERS } from "../../../types/dsql";
|
||||
|
||||
/**
|
||||
* # Update API User Function
|
||||
*/
|
||||
export default async function apiResetUserPassword({
|
||||
updatedUserId,
|
||||
database,
|
||||
dbUserId,
|
||||
newPassword,
|
||||
encryptionKey,
|
||||
}: ResetPasswordParams): Promise<APIResponseObject> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
const existingUserQuery = `SELECT * FROM users WHERE id = ?`;
|
||||
const existingUserValues = [updatedUserId];
|
||||
|
||||
const existingUser = (await dbHandler({
|
||||
query: existingUserQuery,
|
||||
values: existingUserValues,
|
||||
database: dbFullName,
|
||||
})) as any[];
|
||||
|
||||
if (!existingUser?.[0]) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "User not found",
|
||||
};
|
||||
}
|
||||
|
||||
const newPasswordHashed = hashPassword({
|
||||
password: newPassword,
|
||||
encryptionKey,
|
||||
});
|
||||
|
||||
const updateUser = await updateDbEntry<DSQL_DATASQUIREL_USERS>({
|
||||
dbFullName,
|
||||
tableName: "users",
|
||||
identifierColumnName: "id",
|
||||
identifierValue: updatedUserId,
|
||||
data: { password: newPasswordHashed },
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
payload: updateUser,
|
||||
};
|
||||
}
|
||||
@@ -1,24 +1,14 @@
|
||||
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 { CookieObject, SendOneTimeCodeEmailResponse } from "../../../types";
|
||||
|
||||
type Param = {
|
||||
email: string;
|
||||
database: string;
|
||||
email_login_field?: string;
|
||||
mail_domain?: string;
|
||||
mail_port?: number;
|
||||
sender?: string;
|
||||
mail_username?: string;
|
||||
mail_password?: string;
|
||||
html: string;
|
||||
response?: http.ServerResponse & { [s: string]: any };
|
||||
extraCookies?: CookieObject[];
|
||||
};
|
||||
import {
|
||||
APIResponseObject,
|
||||
APISendEmailCodeFunctionParams,
|
||||
CookieObject,
|
||||
} from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
|
||||
/**
|
||||
* # Send Email Login Code
|
||||
@@ -35,22 +25,26 @@ export default async function apiSendEmailCode({
|
||||
html,
|
||||
response,
|
||||
extraCookies,
|
||||
}: Param): Promise<SendOneTimeCodeEmailResponse> {
|
||||
dbUserId,
|
||||
}: APISendEmailCodeFunctionParams): Promise<APIResponseObject> {
|
||||
if (email?.match(/ /)) {
|
||||
return {
|
||||
success: false,
|
||||
msg: "Invalid Email/Password format",
|
||||
};
|
||||
}
|
||||
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
const createdAt = Date.now();
|
||||
|
||||
const foundUserQuery = `SELECT * FROM ${database}.users WHERE email = ?`;
|
||||
const foundUserQuery = `SELECT * FROM users WHERE email = ?`;
|
||||
const foundUserValues = [email];
|
||||
|
||||
let foundUser = (await dbHandler({
|
||||
query: foundUserQuery,
|
||||
values: foundUserValues,
|
||||
database,
|
||||
database: dbFullName,
|
||||
})) as any[];
|
||||
|
||||
if (!foundUser || !foundUser[0]) {
|
||||
@@ -88,10 +82,11 @@ export default async function apiSendEmailCode({
|
||||
|
||||
let mailObject: SendMailOptions = {};
|
||||
|
||||
mailObject["from"] = `"Datasquirel SSO" <${
|
||||
sender || "support@datasquirel.com"
|
||||
}>`;
|
||||
mailObject["sender"] = sender || "support@datasquirel.com";
|
||||
const finalSender =
|
||||
sender || process.env.DSQL_MAIL_EMAIL || "support@datasquirel.com";
|
||||
|
||||
mailObject["from"] = `"Datasquirel SSO" <${finalSender}>`;
|
||||
mailObject["sender"] = finalSender;
|
||||
mailObject["to"] = email;
|
||||
mailObject["subject"] = "One Time Login Code";
|
||||
mailObject["html"] = html.replace(/{{code}}/, tempCode);
|
||||
@@ -100,23 +95,22 @@ export default async function apiSendEmailCode({
|
||||
|
||||
if (!info?.accepted) throw new Error("Mail not Sent!");
|
||||
|
||||
const setTempCodeQuery = `UPDATE ${database}.users SET ${email_login_field} = ? WHERE email = ?`;
|
||||
const setTempCodeQuery = `UPDATE users SET ${email_login_field} = ? WHERE email = ?`;
|
||||
const setTempCodeValues = [tempCode + `-${createdAt}`, email];
|
||||
|
||||
await dbHandler({
|
||||
query: setTempCodeQuery,
|
||||
values: setTempCodeValues,
|
||||
database,
|
||||
database: dbFullName,
|
||||
});
|
||||
|
||||
const resObject: import("../../../types").SendOneTimeCodeEmailResponse =
|
||||
{
|
||||
success: true,
|
||||
code: tempCode,
|
||||
email: email,
|
||||
createdAt,
|
||||
msg: "Success",
|
||||
};
|
||||
const resObject: APIResponseObject = {
|
||||
success: true,
|
||||
code: tempCode,
|
||||
email: email,
|
||||
createdAt,
|
||||
msg: "Success",
|
||||
};
|
||||
|
||||
if (response) {
|
||||
const cookieKeyNames = getAuthCookieNames();
|
||||
|
||||
@@ -1,28 +1,22 @@
|
||||
// @ts-check
|
||||
|
||||
import updateDbEntry from "../../backend/db/updateDbEntry";
|
||||
import encrypt from "../../dsql/encrypt";
|
||||
import hashPassword from "../../dsql/hashPassword";
|
||||
import dbHandler from "../../backend/dbHandler";
|
||||
|
||||
type Param = {
|
||||
payload: { [s: string]: any };
|
||||
dbFullName: string;
|
||||
updatedUserId: string | number;
|
||||
dbSchema?: import("../../../types").DSQL_DatabaseSchemaType;
|
||||
};
|
||||
|
||||
type Return = { success: boolean; payload?: any; msg?: string };
|
||||
import { APIResponseObject, ApiUpdateUserParams } from "../../../types";
|
||||
import grabDbFullName from "../../../utils/grab-db-full-name";
|
||||
|
||||
/**
|
||||
* # Update API User Function
|
||||
*/
|
||||
export default async function apiUpdateUser({
|
||||
payload,
|
||||
dbFullName,
|
||||
updatedUserId,
|
||||
dbSchema,
|
||||
}: Param): Promise<Return> {
|
||||
database,
|
||||
dbUserId,
|
||||
}: ApiUpdateUserParams): Promise<APIResponseObject> {
|
||||
const dbFullName = grabDbFullName({ dbName: database, userId: dbUserId });
|
||||
|
||||
const existingUserQuery = `SELECT * FROM ${dbFullName}.users WHERE id = ?`;
|
||||
const existingUserValues = [updatedUserId];
|
||||
|
||||
@@ -53,8 +47,7 @@ export default async function apiUpdateUser({
|
||||
}
|
||||
})();
|
||||
|
||||
/** @type {any} */
|
||||
const finalData: any = {};
|
||||
const finalData: { [s: string]: any } = {};
|
||||
|
||||
reqBodyKeys.forEach((key) => {
|
||||
const targetFieldSchema = targetTableSchema?.fields?.find(
|
||||
|
||||
Reference in New Issue
Block a user