From e7fafc1bb61c2315c026364b9e389e44922bb2dc Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sat, 28 Dec 2024 08:03:57 +0100 Subject: [PATCH] Updates --- .../api/users/social/api-github-login.d.ts | 7 ++-- .../api/users/social/api-github-login.js | 14 ++++--- .../api/users/social/api-google-login.js | 7 +++- package-shared/types/index.d.ts | 3 ++ package-shared/types/index.ts | 1 + package.json | 2 +- users/social/github-auth.d.ts | 6 ++- users/social/github-auth.js | 37 ++----------------- users/social/google-auth.d.ts | 6 ++- users/social/google-auth.js | 4 ++ 10 files changed, 41 insertions(+), 46 deletions(-) diff --git a/package-shared/functions/api/users/social/api-github-login.d.ts b/package-shared/functions/api/users/social/api-github-login.d.ts index 13ff3ea..2e78000 100644 --- a/package-shared/functions/api/users/social/api-github-login.d.ts +++ b/package-shared/functions/api/users/social/api-github-login.d.ts @@ -1,11 +1,12 @@ -declare function _exports({ code, clientId, clientSecret, database, additionalFields, res, email, userId, }: { +declare function _exports({ code, clientId, clientSecret, database, additionalFields, email, additionalData, }: { code?: string; clientId?: string; clientSecret?: string; database?: string; additionalFields?: string[]; - res?: any; + additionalData?: { + [x: string]: string | number; + }; email?: string; - userId?: string | number; }): Promise; export = _exports; diff --git a/package-shared/functions/api/users/social/api-github-login.js b/package-shared/functions/api/users/social/api-github-login.js index 0254848..93fbccd 100644 --- a/package-shared/functions/api/users/social/api-github-login.js +++ b/package-shared/functions/api/users/social/api-github-login.js @@ -12,9 +12,8 @@ const camelJoinedtoCamelSpace = require("../../../../utils/camelJoinedtoCamelSpa * @param {string} [param.clientSecret] * @param {string} [param.database] * @param {string[]} [param.additionalFields] - * @param {any} [param.res] + * @param {Object} [param.additionalData] * @param {string} [param.email] - * @param {string | number} [param.userId] * * @returns {Promise} */ @@ -24,9 +23,8 @@ module.exports = async function apiGithubLogin({ clientSecret, database, additionalFields, - res, email, - userId, + additionalData, }) { if (!code || !clientId || !clientSecret || !database) { return { @@ -73,7 +71,7 @@ module.exports = async function apiGithubLogin({ ? targetName?.split("-") : [targetName]; - const payload = { + let payload = { email: gitHubUser.email, first_name: camelJoinedtoCamelSpace(nameArray[0]), last_name: camelJoinedtoCamelSpace(nameArray[1]), @@ -84,10 +82,14 @@ module.exports = async function apiGithubLogin({ username: "github-user-" + socialId, }; + if (additionalData) { + payload = { ...payload, ...additionalData }; + } + const loggedInGithubUser = await handleSocialDb({ database, email: gitHubUser.email, - payload: payload, + payload, social_platform: "github", social_id: socialId, supEmail: email, diff --git a/package-shared/functions/api/users/social/api-google-login.js b/package-shared/functions/api/users/social/api-google-login.js index 9b085cc..b185a6e 100644 --- a/package-shared/functions/api/users/social/api-google-login.js +++ b/package-shared/functions/api/users/social/api-google-login.js @@ -9,6 +9,7 @@ module.exports = async function apiGoogleLogin({ token, database, additionalFields, + additionalData, }) { try { /** @type {import("../../../../types").GoogleOauth2User | undefined} */ @@ -59,7 +60,7 @@ module.exports = async function apiGoogleLogin({ const { given_name, family_name, email, sub, picture } = gUser; /** @type {Object} */ - const payloadObject = { + let payloadObject = { email: email, first_name: given_name, last_name: family_name, @@ -70,6 +71,10 @@ module.exports = async function apiGoogleLogin({ username: `google-user-${sub}`, }; + if (additionalData) { + payloadObject = { ...payloadObject, ...additionalData }; + } + const loggedInGoogleUser = await handleSocialDb({ database, email: email || "", diff --git a/package-shared/types/index.d.ts b/package-shared/types/index.d.ts index cb2431c..f52138c 100644 --- a/package-shared/types/index.d.ts +++ b/package-shared/types/index.d.ts @@ -1057,6 +1057,9 @@ export type APIGoogleLoginFunctionParams = { token: string; database: string; additionalFields?: string[]; + additionalData?: { + [key: string]: string | number; + }; }; export type APIGoogleLoginFunction = (params: APIGoogleLoginFunctionParams) => Promise; /** diff --git a/package-shared/types/index.ts b/package-shared/types/index.ts index 869b2eb..61b1231 100644 --- a/package-shared/types/index.ts +++ b/package-shared/types/index.ts @@ -1265,6 +1265,7 @@ export type APIGoogleLoginFunctionParams = { token: string; database: string; additionalFields?: string[]; + additionalData?: { [key: string]: string | number }; }; export type APIGoogleLoginFunction = ( diff --git a/package.json b/package.json index c556269..8be49d1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "3.2.2", + "version": "3.2.3", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { diff --git a/users/social/github-auth.d.ts b/users/social/github-auth.d.ts index 87cab88..a730c63 100644 --- a/users/social/github-auth.d.ts +++ b/users/social/github-auth.d.ts @@ -29,11 +29,12 @@ export = githubAuth; * @param {string} params.encryptionKey - Encryption key * @param {string} params.encryptionSalt - Encryption salt * @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object + * @param {Object} [params.additionalData] - Additional Data to by added on creation of User * @param {boolean} [params.user_id] - User ID * * @returns { Promise } */ -declare function githubAuth({ key, code, email, database, clientId, clientSecret, response, encryptionKey, encryptionSalt, additionalFields, user_id, }: { +declare function githubAuth({ key, code, email, database, clientId, clientSecret, response, encryptionKey, encryptionSalt, additionalFields, user_id, additionalData, }: { key: string; code: string; email: string | null; @@ -44,6 +45,9 @@ declare function githubAuth({ key, code, email, database, clientId, clientSecret encryptionKey: string; encryptionSalt: string; additionalFields?: string[]; + additionalData?: { + [x: string]: string | number; + }; user_id?: boolean; }): Promise; declare namespace githubAuth { diff --git a/users/social/github-auth.js b/users/social/github-auth.js index 48a064c..8d5be85 100644 --- a/users/social/github-auth.js +++ b/users/social/github-auth.js @@ -45,6 +45,7 @@ const apiGithubLogin = require("../../package-shared/functions/api/users/social/ * @param {string} params.encryptionKey - Encryption key * @param {string} params.encryptionSalt - Encryption salt * @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object + * @param {Object} [params.additionalData] - Additional Data to by added on creation of User * @param {boolean} [params.user_id] - User ID * * @returns { Promise } @@ -61,6 +62,7 @@ async function githubAuth({ encryptionSalt, additionalFields, user_id, + additionalData, }) { /** * Check inputs @@ -70,14 +72,6 @@ async function githubAuth({ const grabedHostNames = grabHostNames(); const { host, port, scheme } = grabedHostNames; - if (!key || key?.match(/ /)) { - return { - success: false, - user: null, - msg: "Please enter API full access Key", - }; - } - if (!code || code?.match(/ /)) { return { success: false, @@ -102,30 +96,6 @@ async function githubAuth({ }; } - if (!response || !response?.setHeader) { - return { - success: false, - user: null, - msg: "Please provide a valid HTTPS response object", - }; - } - - if (!encryptionKey || encryptionKey?.match(/ /)) { - return { - success: false, - user: null, - msg: "Please provide a valid encryption key", - }; - } - - if (!encryptionSalt || encryptionSalt?.match(/ /)) { - return { - success: false, - user: null, - msg: "Please provide a valid encryption salt", - }; - } - //////////////////////////////////////// //////////////////////////////////////// //////////////////////////////////////// @@ -173,8 +143,8 @@ async function githubAuth({ clientId, clientSecret, additionalFields, - res: response, database: DSQL_DB_NAME, + additionalData, }); } else { /** @@ -191,6 +161,7 @@ async function githubAuth({ clientSecret, database, additionalFields, + additionalData, }); const httpsRequest = scheme.request( diff --git a/users/social/google-auth.d.ts b/users/social/google-auth.d.ts index 2c532df..6e96284 100644 --- a/users/social/google-auth.d.ts +++ b/users/social/google-auth.d.ts @@ -13,12 +13,13 @@ export = googleAuth; * @param {string} [params.encryptionKey] - Encryption key * @param {string} [params.encryptionSalt] - Encryption salt * @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object + * @param {Object} [params.additionalData] - Additional Data to by added on creation of User * @param {string | number} [params.apiUserID] - API user ID * @param {boolean} [params.useLocal] - Whether to use a remote database instead of API * * @returns { Promise } */ -declare function googleAuth({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, apiUserID, useLocal, }: { +declare function googleAuth({ key, token, database, response, encryptionKey, encryptionSalt, additionalFields, additionalData, apiUserID, useLocal, }: { key?: string; token: string; database?: string; @@ -26,6 +27,9 @@ declare function googleAuth({ key, token, database, response, encryptionKey, enc encryptionKey?: string; encryptionSalt?: string; additionalFields?: string[]; + additionalData?: { + [x: string]: string | number; + }; apiUserID?: string | number; useLocal?: boolean; }): Promise; diff --git a/users/social/google-auth.js b/users/social/google-auth.js index 8ec11c3..7150fe7 100644 --- a/users/social/google-auth.js +++ b/users/social/google-auth.js @@ -23,6 +23,7 @@ const { * @param {string} [params.encryptionKey] - Encryption key * @param {string} [params.encryptionSalt] - Encryption salt * @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object + * @param {Object} [params.additionalData] - Additional Data to by added on creation of User * @param {string | number} [params.apiUserID] - API user ID * @param {boolean} [params.useLocal] - Whether to use a remote database instead of API * @@ -36,6 +37,7 @@ async function googleAuth({ encryptionKey, encryptionSalt, additionalFields, + additionalData, apiUserID, useLocal, }) { @@ -106,6 +108,7 @@ async function googleAuth({ token, additionalFields, database: DSQL_DB_NAME, + additionalData, }); } else { /** @@ -119,6 +122,7 @@ async function googleAuth({ token, database, additionalFields, + additionalData, }); const httpsRequest = scheme.request(