2024-11-05 11:12:42 +00:00
"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
* / m o d u l e . e x p o r t s = a s y n c f u n c t i o n a d d U s e r s T a b l e T o D b ( { u s e r I d , d a t a b a s e } ) {
/ * *
* Initialize
*
* @ description Initialize
* / c o n s t d b F u l l N a m e = ` d a t a s q u i r e l _ u s e r _ $ { u s e r I d } _ $ { d a t a b a s e } ` ;
/** @type {import("@/package-shared/types").DSQL_TableSchemaType} */ // @ts-ignore
const userPreset = _ _webpack _require _ _ ( 9258 ) ;
try {
/ * *
* Fetch user
*
* @ description Fetch user from db
* / c o n s t u s e r S c h e m a D a t a = g r a b U s e r S c h e m a D a t a ( {
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 ;
}
} ; ////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
2024-11-05 14:18:40 +00:00
/***/ } ) ,
/***/ 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 }
* / c o n s t d e c r y p t = ( e n c r y p t e d S t r i n g ) = > {
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 ;
2024-11-05 11:12:42 +00:00
/***/ } ) ,
/***/ 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
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* / c o n s t f s = _ _ w e b p a c k _ r e q u i r e _ _ ( 7 1 4 7 ) ;
const path = _ _webpack _require _ _ ( 1017 ) ;
const { execSync } = _ _webpack _require _ _ ( 2081 ) ;
/** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ /** ****************************************************************************** */ / * *
* API handler
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* @ type { import ( "next" ) . NextApiHandler }
* / a s y n c f u n c t i o n h a n d l e r ( r e q , r e s ) {
/ * *
* Check method
*
* @ description Check request method and return if invalid
* / i f ( r e q . m e t h o d ! = = " P O S T " ) r e t u r n r e s . j s o n ( {
success : false ,
payload : null ,
msg : "Failed!"
} ) ;
/ * *
* Send Response
*
* @ description Send a boolean response
* / t r y {
/ * *
* User auth
*
* @ description Authenticate user
* / c o n s t d e l e t e d K e y s = f s . r e a d F i l e S y n c ( " . / a p i K e y s / d e l e t e d . t x t " , " u t f 8 " ) ;
/** @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
* / c o n s t s a n i t i z e d R e q B o d y = r e q . b o d y ;
const { payload , database , encryptionKey } = sanitizedReqBody ;
/ * *
* Hash Password
*
* @ description Hash Password
* / i f ( ! p a y l o a d ? . p a s s w o r d ) {
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
* / c o n s t d b F u l l N a m e = ` d a t a s q u i r e l _ u s e r _ $ { u s e r _ i d } _ $ { d a t a b a s e } ` ;
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 ) )
2024-11-05 14:18:40 +00:00
var _ _webpack _exports _ _ = _ _webpack _require _ _ . X ( 0 , [ 2224 , 2163 , 3017 , 3403 , 7547 , 5886 , 5338 , 8326 , 1311 , 8164 , 7638 , 9258 ] , ( ) => ( _ _webpack _exec _ _ ( 1280 ) ) ) ;
2024-11-05 11:12:42 +00:00
module . exports = _ _webpack _exports _ _ ;
} ) ( ) ;