2024-11-05 11:12:42 +00:00
"use strict" ;
exports . id = 7839 ;
exports . ids = [ 7839 ] ;
exports . modules = {
/***/ 7839 :
/***/ ( ( module , _ _unused _webpack _exports , _ _webpack _require _ _ ) => {
// @ts-check
/ * *
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* Imports
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* /
const fs = _ _webpack _require _ _ ( 7147 ) ;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
const addAdminUserOnLogin = _ _webpack _require _ _ ( 613 ) ;
const handleNodemailer = _ _webpack _require _ _ ( 6926 ) ;
const { ServerResponse } = _ _webpack _require _ _ ( 3685 ) ;
const path = _ _webpack _require _ _ ( 1017 ) ;
const addMariadbUser = _ _webpack _require _ _ ( 4294 ) ;
const varDatabaseDbHandler = _ _webpack _require _ _ ( 1311 ) ;
const encrypt = _ _webpack _require _ _ ( 7547 ) ;
const addDbEntry = _ _webpack _require _ _ ( 5338 ) ;
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/ * *
* @ typedef { object } FunctionReturn
* @ property { boolean } success - Did the operation complete successfully or not ?
* @ property { {
* id : number ,
* first _name : string ,
* last _name : string ,
* } | null } user - User payload object : or "null"
* / / * *
* Handle Social User Auth on Datasquirel Database
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
*
* @ description This function handles all social login logic after the social user
* has been authenticated and userpayload is present . The payload MUST contain the
* specified fields because this funciton will create a new user if the authenticated
* user does not exist .
*
* @ param { {
* database ? : string ,
* social _id : string | number ,
* email : string ,
* social _platform : string ,
* payload : any ,
* res ? : ServerResponse ,
* invitation ? : any ,
* supEmail ? : string ,
* additionalFields ? : object ,
* } } params - function parameters inside an object
*
* @ returns { Promise < any > } - Response object
* / 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 h a n d l e S o c i a l D b ( { d a t a b a s e , s o c i a l _ i d , e m a i l , s o c i a l _ p l a t f o r m , p a y l o a d , r e s , i n v i t a t i o n , s u p E m a i l , a d d i t i o n a l F i e l d s , } ) {
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
try {
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
let existingSocialIdUser = await varDatabaseDbHandler ( {
database : database ? database : "datasquirel" ,
queryString : ` SELECT * FROM users WHERE social_id = ? AND social_login='1' AND social_platform = ? ` ,
queryValuesArray : [
social _id . toString ( ) ,
social _platform
]
} ) ;
if ( existingSocialIdUser && existingSocialIdUser [ 0 ] ) {
return await loginSocialUser ( {
user : existingSocialIdUser [ 0 ] ,
social _platform ,
res ,
invitation ,
database ,
additionalFields
} ) ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
const finalEmail = email ? email : supEmail ? supEmail : null ;
if ( ! finalEmail ) {
return {
success : false ,
user : null ,
msg : "No Email Present" ,
social _id ,
social _platform ,
payload
} ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
let existingEmailOnly = await varDatabaseDbHandler ( {
database : database ? database : "datasquirel" ,
queryString : ` SELECT * FROM users WHERE email=' ${ finalEmail } ' `
} ) ;
if ( existingEmailOnly && existingEmailOnly [ 0 ] ) {
return {
user : null ,
msg : "This Email is already taken" ,
alert : true
} ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
const foundUser = await varDatabaseDbHandler ( {
database : database ? database : "datasquirel" ,
queryString : ` SELECT * FROM users WHERE email=' ${ finalEmail } ' AND social_login='1' AND social_platform=' ${ social _platform } ' AND social_id=' ${ social _id } ' `
} ) ;
if ( foundUser && foundUser [ 0 ] ) {
return await loginSocialUser ( {
user : payload ,
social _platform ,
res ,
invitation ,
database ,
additionalFields
} ) ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
const socialHashedPassword = encrypt ( social _id . toString ( ) ) ;
/** @type {any} */ const data = {
social _login : "1" ,
verification _status : supEmail ? "0" : "1" ,
password : socialHashedPassword
} ;
Object . keys ( payload ) . forEach ( ( key ) => {
data [ key ] = payload [ key ] ;
} ) ;
/** @type {any} */ const newUser = await addDbEntry ( {
dbContext : database ? "Dsql User" : undefined ,
paradigm : database ? "Full Access" : undefined ,
dbFullName : database ? database : "datasquirel" ,
tableName : "users" ,
duplicateColumnName : "email" ,
duplicateColumnValue : finalEmail ,
data : {
... data ,
email : finalEmail
}
} ) ;
if ( newUser ? . insertId ) {
if ( ! database ) {
/ * *
* Add a Mariadb User for this User
* / a w a i t a d d M a r i a d b U s e r ( {
userId : newUser . insertId
} ) ;
}
const newUserQueried = await varDatabaseDbHandler ( {
database : database ? database : "datasquirel" ,
queryString : ` SELECT * FROM users WHERE id=' ${ newUser . insertId } ' `
} ) ;
if ( ! newUserQueried || ! newUserQueried [ 0 ] ) return {
user : null ,
msg : "User Insertion Failed!"
} ;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
if ( supEmail && database ? . match ( /^datasquirel$/ ) ) {
/ * *
* Send email Verification
*
* @ description Send verification email to newly created agent
* / l e t g e n e r a t e d T o k e n = e n c r y p t ( J S O N . s t r i n g i f y ( {
id : newUser . insertId ,
email : supEmail ,
dateCode : Date . now ( )
} ) ) ;
handleNodemailer ( {
to : supEmail ,
subject : "Verify Email Address" ,
text : "Please click the link to verify your email address" ,
html : fs . readFileSync ( "./email/send-email-verification-link.html" , "utf8" ) . replace ( /{{host}}/ , process . env . DSQL _HOST || "" ) . replace ( /{{token}}/ , generatedToken || "" )
} ) . then ( ( mail ) => { } ) ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
const STATIC _ROOT = process . env . DSQL _STATIC _SERVER _DIR ;
if ( ! STATIC _ROOT ) {
console . log ( "Static File ENV not Found!" ) ;
return null ;
}
/ * *
* Create new user folder and file
*
* @ description Create new user folder and file
* / if (!database || database?.match(/ ^ datasquirel$ / ) ) {
2024-11-05 14:18:40 +00:00
let newUserSchemaFolderPath = ` ${ process . env . DSQL _USER _DB _SCHEMA _PATH } /user- ${ newUser . insertId } ` ;
2024-11-05 11:12:42 +00:00
let newUserMediaFolderPath = path . join ( STATIC _ROOT , ` images/user-images/user- ${ newUser . insertId } ` ) ;
fs . mkdirSync ( newUserSchemaFolderPath ) ;
fs . mkdirSync ( newUserMediaFolderPath ) ;
fs . writeFileSync ( ` ${ newUserSchemaFolderPath } /main.json ` , JSON . stringify ( [ ] ) , "utf8" ) ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
return await loginSocialUser ( {
user : newUserQueried [ 0 ] ,
social _platform ,
res ,
invitation ,
database ,
additionalFields
} ) ;
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
} else {
console . log ( "Social User Failed to insert in 'handleSocialDb.js' backend function =>" , newUser ) ;
return {
success : false ,
user : null ,
msg : "Social User Failed to insert in 'handleSocialDb.js' backend function => " ,
newUser : newUser
} ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
} catch ( /** @type {any} */ error ) {
console . log ( "ERROR in 'handleSocialDb.js' backend function =>" , error . message ) ;
return {
success : false ,
user : null ,
error : error . message
} ;
// serverError({
// component: "/functions/backend/social-login/handleSocialDb.js - main-catch-error",
// message: error.message,
// user: { first_name, last_name },
// });
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
return {
user : null ,
msg : "User Login Failed!"
} ;
} ;
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
/ * *
* Function to login social user
* === === === === === === === === === === === === === === === === === === === === === === === === === ===
* @ description This function logs in the user after 'handleSocialDb' function finishes
* the user creation or confirmation process
*
* @ async
*
* @ param { object } params - function parameters inside an object
* @ param { {
* first _name : string ,
* last _name : string ,
* email : string ,
* social _id : string | number ,
* } } params . user - user object
* @ param { string } params . social _platform - Whether its "google" or "facebook" or "github"
* @ param { ServerResponse } [ params . res ] - Https response object
* @ param { any } [ params . invitation ] - A query object if user was invited
* @ param { string } [ params . database ] - Target Database
* @ param { object } [ params . additionalFields ] - Additional fields to be added to the user payload
*
* @ returns { Promise < any > }
* / a s y n c f u n c t i o n l o g i n S o c i a l U s e r ( { u s e r , s o c i a l _ p l a t f o r m , r e s , i n v i t a t i o n , d a t a b a s e , a d d i t i o n a l F i e l d s , } ) {
const foundUser = await varDatabaseDbHandler ( {
database : database ? database : "datasquirel" ,
queryString : ` SELECT * FROM users WHERE email=' ${ user . email } ' AND social_id=' ${ user . social _id } ' AND social_platform=' ${ social _platform } ' `
} ) ;
if ( ! foundUser ? . [ 0 ] ) return {
success : false ,
user : null
} ;
let csrfKey = Math . random ( ) . toString ( 36 ) . substring ( 2 ) + "-" + Math . random ( ) . toString ( 36 ) . substring ( 2 ) ;
/** @type {any} */ let userPayload = {
id : foundUser [ 0 ] . id ,
type : foundUser [ 0 ] . type || "" ,
stripe _id : foundUser [ 0 ] . stripe _id || "" ,
first _name : foundUser [ 0 ] . first _name ,
last _name : foundUser [ 0 ] . last _name ,
username : foundUser [ 0 ] . username ,
email : foundUser [ 0 ] . email ,
social _id : foundUser [ 0 ] . social _id ,
image : foundUser [ 0 ] . image ,
image _thumbnail : foundUser [ 0 ] . image _thumbnail ,
verification _status : foundUser [ 0 ] . verification _status ,
social _login : foundUser [ 0 ] . social _login ,
social _platform : foundUser [ 0 ] . social _platform ,
csrf _k : csrfKey ,
logged _in _status : true ,
date : Date . now ( )
} ;
if ( additionalFields && Object . keys ( additionalFields ) . length > 0 ) {
Object . keys ( additionalFields ) . forEach ( ( key ) => {
userPayload [ key ] = foundUser [ 0 ] [ key ] ;
} ) ;
}
let encryptedPayload = encrypt ( JSON . stringify ( userPayload ) ) ;
if ( res ? . setHeader ) {
res . setHeader ( "Set-Cookie" , [
` datasquirelAuthKey= ${ encryptedPayload } ;samesite=strict;path=/;HttpOnly=true;Secure=true ` ,
` csrf= ${ csrfKey } ;samesite=strict;path=/;HttpOnly=true ` ,
] ) ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
if ( invitation && ( ! database || database ? . match ( /^datasquirel$/ ) ) ) {
addAdminUserOnLogin ( {
query : invitation ,
user : userPayload
} ) ;
}
////////////////////////////////////////////////
////////////////////////////////////////////////
////////////////////////////////////////////////
return {
success : true ,
user : userPayload
} ;
}
/***/ } )
} ;
;