422 lines
16 KiB
JavaScript
422 lines
16 KiB
JavaScript
"use strict";
|
|
(() => {
|
|
var exports = {};
|
|
exports.id = 4020;
|
|
exports.ids = [4020];
|
|
exports.modules = {
|
|
|
|
/***/ 2029:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("datasquirel/functions/hashPassword");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 6517:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("lodash");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 6109:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("sanitize-html");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 2261:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("serverless-mysql");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 4300:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("buffer");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 2081:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("child_process");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 6113:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("crypto");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 7147:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("fs");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 1017:
|
|
/***/ ((module) => {
|
|
|
|
module.exports = require("path");
|
|
|
|
/***/ }),
|
|
|
|
/***/ 1459:
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
// @ts-check
|
|
/**
|
|
* ==============================================================================
|
|
* Imports
|
|
* ==============================================================================
|
|
*/
|
|
const fs = __webpack_require__(7147);
|
|
const path = __webpack_require__(1017);
|
|
const { execSync } = __webpack_require__(2081);
|
|
const serverError = __webpack_require__(2163);
|
|
const DB_HANDLER = __webpack_require__(2224);
|
|
const { default: grabUserSchemaData } = __webpack_require__(8164);
|
|
const { default: setUserSchemaData } = __webpack_require__(7638);
|
|
const addDbEntry = __webpack_require__(5338);
|
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
|
* ==============================================================================
|
|
* Main Function
|
|
* ==============================================================================
|
|
* @param {object} params
|
|
* @param {number} params.userId - user id
|
|
* @param {string} params.database
|
|
*
|
|
* @returns {Promise<any>} new user auth object payload
|
|
*/ module.exports = async function addUsersTableToDb({ userId , database }) {
|
|
/**
|
|
* Initialize
|
|
*
|
|
* @description Initialize
|
|
*/ const dbFullName = `datasquirel_user_${userId}_${database}`;
|
|
/** @type {import("@/package-shared/types").DSQL_TableSchemaType} */ // @ts-ignore
|
|
const userPreset = __webpack_require__(9258);
|
|
try {
|
|
/**
|
|
* Fetch user
|
|
*
|
|
* @description Fetch user from db
|
|
*/ const userSchemaData = grabUserSchemaData({
|
|
userId
|
|
});
|
|
if (!userSchemaData) throw new Error("User schema data not found!");
|
|
let targetDatabase = userSchemaData.filter((db)=>db.dbSlug === database)[0];
|
|
let existingTableIndex;
|
|
// @ts-ignore
|
|
let existingTable = targetDatabase.tables.filter((table, index)=>{
|
|
if (table.tableName === "users") {
|
|
existingTableIndex = index;
|
|
return true;
|
|
}
|
|
});
|
|
if (existingTable && existingTable[0] && existingTableIndex) {
|
|
targetDatabase.tables[existingTableIndex] = userPreset;
|
|
} else {
|
|
targetDatabase.tables.push(userPreset);
|
|
}
|
|
setUserSchemaData({
|
|
schemaData: userSchemaData,
|
|
userId
|
|
});
|
|
////////////////////////////////////////
|
|
const targetDb = await DB_HANDLER(`SELECT id FROM user_databases WHERE user_id=? AND db_slug=?`, [
|
|
userId,
|
|
database
|
|
]);
|
|
if (targetDb && targetDb[0]) {
|
|
const newTableEntry = await addDbEntry({
|
|
dbFullName: "datasquirel",
|
|
tableName: "user_database_tables",
|
|
data: {
|
|
user_id: userId,
|
|
db_id: targetDb[0].id,
|
|
db_slug: database,
|
|
table_name: "Users",
|
|
table_slug: "users"
|
|
}
|
|
});
|
|
}
|
|
////////////////////////////////////////
|
|
const targetPath = path.join(process.cwd(), "/shell");
|
|
const dbShellUpdate = execSync(`node createDbFromSchema.js --user ${userId} --database ${dbFullName}`, {
|
|
cwd: targetPath
|
|
});
|
|
return dbShellUpdate.toString();
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
} catch (/** @type {any} */ error) {
|
|
serverError({
|
|
component: "addUsersTableToDb",
|
|
message: error.message,
|
|
user: {
|
|
id: userId
|
|
}
|
|
});
|
|
return error.message;
|
|
}
|
|
}; ////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
////////////////////////////////////////
|
|
|
|
|
|
/***/ }),
|
|
|
|
/***/ 5425:
|
|
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {
|
|
|
|
// @ts-check
|
|
|
|
const { scryptSync , createDecipheriv } = __webpack_require__(6113);
|
|
const { Buffer } = __webpack_require__(4300);
|
|
/**
|
|
* @param {string} encryptedString
|
|
* @returns {string | null}
|
|
*/ const decrypt = (encryptedString)=>{
|
|
const algorithm = "aes-192-cbc";
|
|
const password = process.env.DSQL_ENCRYPTION_PASSWORD || "";
|
|
const salt = process.env.DSQL_ENCRYPTION_SALT || "";
|
|
let key = scryptSync(password, salt, 24);
|
|
let iv = Buffer.alloc(16, 0);
|
|
// @ts-ignore
|
|
const decipher = createDecipheriv(algorithm, key, iv);
|
|
try {
|
|
let decrypted = decipher.update(encryptedString, "hex", "utf8");
|
|
decrypted += decipher.final("utf8");
|
|
return decrypted;
|
|
} catch (error) {
|
|
return null;
|
|
}
|
|
};
|
|
module.exports = decrypt;
|
|
|
|
|
|
/***/ }),
|
|
|
|
/***/ 1280:
|
|
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
|
|
|
|
__webpack_require__.r(__webpack_exports__);
|
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
|
|
/* harmony export */ "default": () => (/* binding */ handler)
|
|
/* harmony export */ });
|
|
/* harmony import */ var _functions_backend_addUsersTableToDb__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1459);
|
|
/* harmony import */ var _functions_backend_addUsersTableToDb__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_addUsersTableToDb__WEBPACK_IMPORTED_MODULE_0__);
|
|
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5338);
|
|
/* harmony import */ var _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_1__);
|
|
/* harmony import */ var _package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5425);
|
|
/* harmony import */ var _package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_2__);
|
|
/* harmony import */ var datasquirel_functions_hashPassword__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(2029);
|
|
/* harmony import */ var datasquirel_functions_hashPassword__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(datasquirel_functions_hashPassword__WEBPACK_IMPORTED_MODULE_3__);
|
|
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(2163);
|
|
/* harmony import */ var _functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4__);
|
|
/* harmony import */ var _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(1311);
|
|
/* harmony import */ var _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_5___default = /*#__PURE__*/__webpack_require__.n(_package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_5__);
|
|
// @ts-check
|
|
/**
|
|
* ==============================================================================
|
|
* Imports
|
|
* ==============================================================================
|
|
*/ const fs = __webpack_require__(7147);
|
|
const path = __webpack_require__(1017);
|
|
const { execSync } = __webpack_require__(2081);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /**
|
|
* API handler
|
|
* ==============================================================================
|
|
* @type {import("next").NextApiHandler}
|
|
*/ async function handler(req, res) {
|
|
/**
|
|
* Check method
|
|
*
|
|
* @description Check request method and return if invalid
|
|
*/ if (req.method !== "POST") return res.json({
|
|
success: false,
|
|
payload: null,
|
|
msg: "Failed!"
|
|
});
|
|
/**
|
|
* Send Response
|
|
*
|
|
* @description Send a boolean response
|
|
*/ try {
|
|
/**
|
|
* User auth
|
|
*
|
|
* @description Authenticate user
|
|
*/ const deletedKeys = fs.readFileSync("./apiKeys/deleted.txt", "utf8");
|
|
/** @type {string} */ // @ts-ignore
|
|
const authorization = req.headers.authorization;
|
|
if (deletedKeys.includes(authorization)) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "Key Inactive!"
|
|
});
|
|
}
|
|
const userJSON = _package_shared_functions_backend_decrypt__WEBPACK_IMPORTED_MODULE_2___default()(authorization);
|
|
if (!userJSON) throw new Error("Invalid Credentials");
|
|
const user = JSON.parse(userJSON);
|
|
const { user_id , full_access , csrf } = user;
|
|
if (!full_access || !csrf) return res.json({
|
|
success: false,
|
|
msg: "Unauthorized"
|
|
});
|
|
/**
|
|
* User auth
|
|
*
|
|
* @description Authenticate user
|
|
*/ const sanitizedReqBody = req.body;
|
|
const { payload , database , encryptionKey } = sanitizedReqBody;
|
|
/**
|
|
* Hash Password
|
|
*
|
|
* @description Hash Password
|
|
*/ if (!payload?.password) {
|
|
res.json({
|
|
success: false,
|
|
msg: `Password is required to create an account`
|
|
});
|
|
return;
|
|
}
|
|
const hashedPassword = datasquirel_functions_hashPassword__WEBPACK_IMPORTED_MODULE_3___default()({
|
|
encryptionKey: encryptionKey,
|
|
password: String(payload.password)
|
|
});
|
|
payload.password = hashedPassword;
|
|
/**
|
|
* Input Validation
|
|
*
|
|
* @description Input Validation
|
|
* @required - payload.first_name
|
|
* @required - payload.last_name
|
|
* @required - payload.email
|
|
* @required - payload.password
|
|
*/ const dbFullName = `datasquirel_user_${user_id}_${database}`;
|
|
let fields = await _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_5___default()({
|
|
queryString: `SHOW COLUMNS FROM users`,
|
|
database: dbFullName
|
|
});
|
|
if (!fields) {
|
|
const newTable = await _functions_backend_addUsersTableToDb__WEBPACK_IMPORTED_MODULE_0___default()({
|
|
userId: user_id,
|
|
database: database
|
|
});
|
|
fields = await _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_5___default()({
|
|
queryString: `SHOW COLUMNS FROM users`,
|
|
database: dbFullName
|
|
});
|
|
}
|
|
if (!fields) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "Could not create users table"
|
|
});
|
|
}
|
|
const fieldsTitles = fields.map((/** @type {any} */ fieldObject)=>fieldObject.Field);
|
|
let invalidField = null;
|
|
for(let i = 0; i < Object.keys(payload).length; i++){
|
|
const key = Object.keys(payload)[i];
|
|
if (!fieldsTitles.includes(key)) {
|
|
invalidField = key;
|
|
break;
|
|
}
|
|
}
|
|
if (invalidField) {
|
|
res.json({
|
|
success: false,
|
|
msg: `${invalidField} is not a valid field!`
|
|
});
|
|
return;
|
|
}
|
|
const existingUser = await _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_5___default()({
|
|
queryString: `SELECT * FROM users WHERE email = ?${payload.username ? " OR username = ?" : ""}`,
|
|
queryValuesArray: payload.username ? [
|
|
payload.email,
|
|
payload.username
|
|
] : [
|
|
payload.email
|
|
],
|
|
database: dbFullName
|
|
});
|
|
console.log(existingUser);
|
|
if (existingUser?.[0]) {
|
|
return res.json({
|
|
success: false,
|
|
msg: "User Already Exists",
|
|
payload: null
|
|
});
|
|
}
|
|
const addUser = await _package_shared_functions_backend_db_addDbEntry__WEBPACK_IMPORTED_MODULE_1___default()({
|
|
dbContext: "Dsql User",
|
|
paradigm: "Full Access",
|
|
dbFullName: dbFullName,
|
|
tableName: "users",
|
|
data: {
|
|
...payload,
|
|
image: "/images/user_images/user-preset.png",
|
|
image_thumbnail: "/images/user_images/user-preset-thumbnail.png"
|
|
}
|
|
});
|
|
if (addUser?.insertId) {
|
|
const newlyAddedUser = await _package_shared_functions_backend_varDatabaseDbHandler__WEBPACK_IMPORTED_MODULE_5___default()({
|
|
queryString: `SELECT id,first_name,last_name,email,username,phone,image,image_thumbnail,city,state,country,zip_code,address,verification_status,more_user_data FROM users WHERE id='${addUser.insertId}'`,
|
|
database: dbFullName
|
|
});
|
|
res.json({
|
|
success: true,
|
|
payload: newlyAddedUser[0]
|
|
});
|
|
} else {
|
|
res.json({
|
|
success: false,
|
|
msg: "Could not create user",
|
|
sqlResult: addUser,
|
|
payload: null
|
|
});
|
|
}
|
|
////////////////////////////////////////
|
|
} catch (/** @type {any} */ error) {
|
|
////////////////////////////////////////
|
|
_functions_backend_serverError__WEBPACK_IMPORTED_MODULE_4___default()({
|
|
component: "/api/user/add-user/main-catch-error",
|
|
message: error.message,
|
|
user: {}
|
|
});
|
|
res.json({
|
|
success: false,
|
|
msg: "Wrong Credentials"
|
|
});
|
|
////////////////////////////////////////
|
|
}
|
|
}
|
|
|
|
|
|
/***/ })
|
|
|
|
};
|
|
;
|
|
|
|
// load runtime
|
|
var __webpack_require__ = require("../../../webpack-api-runtime.js");
|
|
__webpack_require__.C(exports);
|
|
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
|
|
var __webpack_exports__ = __webpack_require__.X(0, [2224,2163,3017,3403,7547,5886,5338,8326,1311,8164,7638,9258], () => (__webpack_exec__(1280)));
|
|
module.exports = __webpack_exports__;
|
|
|
|
})(); |