2023-09-21 14:00:04 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ==============================================================================
|
|
|
|
* Imports
|
|
|
|
* ==============================================================================
|
|
|
|
*/
|
|
|
|
const http = require("http");
|
|
|
|
const https = require("https");
|
|
|
|
const encrypt = require("../../../functions/encrypt");
|
|
|
|
const camelJoinedtoCamelSpace = require("../../engine/utils/camelJoinedtoCamelSpace");
|
|
|
|
const githubLogin = require("./utils/githubLogin");
|
|
|
|
const handleSocialDb = require("./utils/handleSocialDb");
|
|
|
|
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
/** ****************************************************************************** */
|
|
|
|
|
|
|
|
const database = process.env.DSQL_DB_NAME || "";
|
|
|
|
const encryptionKey = process.env.DSQL_ENCRYPTION_KEY || "";
|
|
|
|
const encryptionSalt = process.env.DSQL_ENCRYPTION_SALT || "";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SERVER FUNCTION: Login with google Function
|
|
|
|
* ==============================================================================
|
|
|
|
*
|
|
|
|
* @async
|
|
|
|
*
|
|
|
|
* @param {object} params - main params object
|
|
|
|
* @param {http.ServerResponse} params.res - HTTPS response object
|
|
|
|
* @param {string} params.code
|
|
|
|
* @param {string} [params.email]
|
|
|
|
* @param {string} params.clientId
|
|
|
|
* @param {string} params.clientSecret
|
|
|
|
* @param {object} [params.additionalFields]
|
2024-10-18 05:45:25 +00:00
|
|
|
* @param {import("@/package-shared/types").DSQL_DatabaseSchemaType} params.dbSchema
|
2023-09-21 14:00:04 +00:00
|
|
|
*/
|
2024-10-14 06:49:01 +00:00
|
|
|
async function localGithubAuth({
|
|
|
|
res,
|
|
|
|
code,
|
|
|
|
email,
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
additionalFields,
|
|
|
|
dbSchema,
|
|
|
|
}) {
|
2023-09-21 14:00:04 +00:00
|
|
|
try {
|
|
|
|
/**
|
|
|
|
* User auth
|
|
|
|
*
|
|
|
|
* @description Authenticate user
|
|
|
|
*/
|
|
|
|
if (!code || !clientId || !clientSecret) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
msg: "Missing query params",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-10-14 06:49:01 +00:00
|
|
|
if (
|
|
|
|
typeof code !== "string" ||
|
|
|
|
typeof clientId !== "string" ||
|
|
|
|
typeof clientSecret !== "string" ||
|
|
|
|
typeof database !== "string"
|
|
|
|
) {
|
2023-09-21 14:00:04 +00:00
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
msg: "Wrong Parameters",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new user folder and file
|
|
|
|
*
|
|
|
|
* @description Create new user folder and file
|
|
|
|
*/
|
|
|
|
const gitHubUser = await githubLogin({
|
|
|
|
code: code,
|
|
|
|
clientId: clientId,
|
|
|
|
clientSecret: clientSecret,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!gitHubUser) {
|
|
|
|
return {
|
|
|
|
success: false,
|
|
|
|
msg: "No github user returned",
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const targetDbName = database;
|
|
|
|
|
|
|
|
const socialId = gitHubUser.name || gitHubUser.id || gitHubUser.login;
|
|
|
|
const targetName = gitHubUser.name || gitHubUser.login;
|
2024-10-14 06:49:01 +00:00
|
|
|
const nameArray = targetName?.match(/ /)
|
|
|
|
? targetName?.split(" ")
|
|
|
|
: targetName?.match(/\-/)
|
|
|
|
? targetName?.split("-")
|
|
|
|
: [targetName];
|
2023-09-21 14:00:04 +00:00
|
|
|
|
|
|
|
const payload = {
|
|
|
|
email: gitHubUser.email,
|
|
|
|
first_name: camelJoinedtoCamelSpace(nameArray[0]) || "",
|
|
|
|
last_name: camelJoinedtoCamelSpace(nameArray[1]) || "",
|
|
|
|
social_id: socialId,
|
|
|
|
social_platform: "github",
|
|
|
|
image: gitHubUser.avatar_url,
|
|
|
|
image_thumbnail: gitHubUser.avatar_url,
|
|
|
|
username: "github-user-" + socialId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields && Object.keys(additionalFields).length > 0) {
|
|
|
|
Object.keys(additionalFields).forEach((key) => {
|
|
|
|
// @ts-ignore
|
|
|
|
payload[key] = additionalFields[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const loggedInGithubUser = await handleSocialDb({
|
|
|
|
database: targetDbName,
|
|
|
|
email: gitHubUser.email,
|
|
|
|
payload: payload,
|
|
|
|
social_platform: "github",
|
|
|
|
res: res,
|
|
|
|
social_id: socialId,
|
|
|
|
supEmail: email,
|
|
|
|
additionalFields,
|
|
|
|
dbSchema: dbSchema,
|
|
|
|
});
|
|
|
|
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
////////////////////////////////////////////////
|
|
|
|
|
|
|
|
return { ...loggedInGithubUser, dsqlUserId: "0" };
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
} catch (/** @type {*} */ error) {
|
|
|
|
console.log("localGithubAuth error", error.message);
|
|
|
|
|
|
|
|
return { success: false, msg: "Failed!" };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
////////////////////////////////////////
|
|
|
|
|
|
|
|
module.exports = localGithubAuth;
|