This commit is contained in:
Tben
2023-08-13 14:00:04 +01:00
parent 98c4c17eb3
commit 43287b0329
25 changed files with 2106 additions and 230 deletions
+88 -60
View File
@@ -7,7 +7,10 @@
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../functions/encrypt");
const localGithubAuth = require("../../engine/user/social/github-auth");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -42,7 +45,7 @@ const encrypt = require("../../functions/encrypt");
* @param {string} params.encryptionSalt - Encryption salt
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
*
* @returns { Promise<FunctionReturn> }
* @returns { Promise<FunctionReturn | undefined> }
*/
async function githubAuth({ key, code, email, database, clientId, clientSecret, response, encryptionKey, encryptionSalt, additionalFields }) {
/**
@@ -110,6 +113,11 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
////////////////////////////////////////
////////////////////////////////////////
/**
* Initialize HTTP response variable
*/
let httpResponse;
/**
* Check for local DB settings
*
@@ -117,73 +125,93 @@ async function githubAuth({ key, code, email, database, clientId, clientSecret,
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {FunctionReturn} - Https response object
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
code,
email,
clientId,
clientSecret,
database,
additionalFields,
});
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
console.log("Reading from local database ...");
if (dbSchema) {
httpResponse = await localGithubAuth({
dbSchema: dbSchema,
code,
email: email || undefined,
clientId,
clientSecret,
additionalFields,
res: response,
});
}
} else {
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {FunctionReturn} - Https response object
*/
httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
code,
email,
clientId,
clientSecret,
database,
additionalFields,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/github-login`,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/github-login`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
try {
resolve(JSON.parse(str));
} catch (error) {
console.log(error);
response.on("end", function () {
try {
resolve(JSON.parse(str));
} catch (error) {
console.log(error);
resolve({
success: false,
user: null,
msg: "Something went wrong",
});
}
});
resolve({
success: false,
user: null,
msg: "Something went wrong",
});
}
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
}
////////////////////////////////////////
////////////////////////////////////////
+78 -48
View File
@@ -7,7 +7,10 @@
*/
const http = require("http");
const https = require("https");
const fs = require("fs");
const path = require("path");
const encrypt = require("../../functions/encrypt");
const localGoogleAuth = require("../../engine/user/social/google-auth");
/** ****************************************************************************** */
/** ****************************************************************************** */
@@ -108,6 +111,11 @@ async function googleAuth({ key, token, database, clientId, response, encryption
////////////////////////////////////////
////////////////////////////////////////
/**
* Initialize HTTP response variable
*/
let httpResponse;
/**
* Check for local DB settings
*
@@ -115,61 +123,83 @@ async function googleAuth({ key, token, database, clientId, response, encryption
*/
const { DSQL_HOST, DSQL_USER, DSQL_PASS, DSQL_DB_NAME, DSQL_KEY, DSQL_REF_DB_NAME, DSQL_FULL_SYNC } = process.env;
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
if (DSQL_HOST?.match(/./) && DSQL_USER?.match(/./) && DSQL_PASS?.match(/./) && DSQL_DB_NAME?.match(/./)) {
/** @type {import("../../types/database-schema.td").DSQL_DatabaseSchemaType | undefined} */
let dbSchema;
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {{ success: boolean, user: import("../../types/user.td").DATASQUIREL_LoggedInUser | null, msg?: string, dsqlUserId?: number } | null } - Https response object
*/
const httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
token,
clientId,
database,
additionalFields,
});
try {
const localDbSchemaPath = path.resolve(process.cwd(), "dsql.schema.json");
dbSchema = JSON.parse(fs.readFileSync(localDbSchemaPath, "utf8"));
} catch (error) {}
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
console.log("Reading from local database ...");
if (dbSchema) {
httpResponse = await localGoogleAuth({
dbSchema: dbSchema,
token,
clientId,
additionalFields,
response: response,
});
}
} else {
////////////////////////////////////////
////////////////////////////////////////
////////////////////////////////////////
/**
* Make https request
*
* @description make a request to datasquirel.com
* @type {{ success: boolean, user: import("../../types/user.td").DATASQUIREL_LoggedInUser | null, msg?: string, dsqlUserId?: number } | null } - Https response object
*/
httpResponse = await new Promise((resolve, reject) => {
const reqPayload = JSON.stringify({
token,
clientId,
database,
additionalFields,
});
const httpsRequest = https.request(
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.from(reqPayload).length,
Authorization: key,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/google-login`,
},
port: 443,
hostname: "datasquirel.com",
path: `/api/user/google-login`,
},
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
/**
* Callback Function
*
* @description https request callback
*/
(response) => {
var str = "";
response.on("data", function (chunk) {
str += chunk;
});
response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("end", function () {
resolve(JSON.parse(str));
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
response.on("error", (err) => {
reject(err);
});
}
);
httpsRequest.write(reqPayload);
httpsRequest.end();
});
}
////////////////////////////////////////
////////////////////////////////////////