From 65d6424dcfce3c3d228057ce0a5dc6de21165455 Mon Sep 17 00:00:00 2001 From: Tben Date: Mon, 7 Aug 2023 04:42:49 +0100 Subject: [PATCH] Type Checking --- package.json | 2 +- users/login-user.js | 29 ++++++++--------------------- users/logout-user.js | 7 +++++-- users/reauth-user.js | 26 ++++++-------------------- users/social/github-auth.js | 3 ++- users/social/google-auth.js | 13 ++++++++----- users/user-auth.js | 21 +++------------------ utils/functions/parseCookies.js | 2 +- 8 files changed, 34 insertions(+), 69 deletions(-) diff --git a/package.json b/package.json index b888879..c22b5e9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "1.2.4", + "version": "1.2.5", "description": "Cloud-based SQL data management tool", "main": "index.js", "scripts": { diff --git a/users/login-user.js b/users/login-user.js index 0da6993..682f339 100644 --- a/users/login-user.js +++ b/users/login-user.js @@ -1,8 +1,11 @@ +// @ts-check + /** * ============================================================================== * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); const encrypt = require("../functions/encrypt"); @@ -16,25 +19,9 @@ const encrypt = require("../functions/encrypt"); /** * @typedef {object} AuthenticatedUser * @property {boolean} success - Did the function run successfully? - * @property {{ - * id: number, - * first_name: string, - * last_name: string, - * username: string, - * email: string, - * phone: string, - * social_id?: string, - * image: string, - * image_thumbnail: string, - * verification_status?: number, - * social_login?: number, - * social_platform?: string, - * csrf_k: string, - * more_data?: string, - * logged_in_status: boolean, - * date: string, - * }} payload - Payload of the response + * @property {import("../types/user.td").DATASQUIREL_LoggedInUser | null} payload - Payload of the response * @property {string} [msg] - An optional message + * @property {number} [userId] - An optional message */ /** @@ -51,7 +38,7 @@ const encrypt = require("../functions/encrypt"); * password: string, * }} params.payload Login Email/Username and Password * @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object - * @param {Object} params.response - Http response object + * @param {http.ServerResponse} params.response - Http response object * @param {String} params.encryptionKey - Encryption Key * @param {String} params.encryptionSalt - Encryption Salt * @@ -96,7 +83,7 @@ async function loginUser({ key, payload, database, additionalFields, response, e * * @description make a request to datasquirel.com * - * @type {{ success: boolean, payload: DATASQUIREL_LoggedInUser | null }} + * @type {{ success: boolean, payload: import("../types/user.td").DATASQUIREL_LoggedInUser | null, userId?: number, msg?: string }} */ const httpResponse = await new Promise((resolve, reject) => { const reqPayload = JSON.stringify({ @@ -165,7 +152,7 @@ async function loginUser({ key, payload, database, additionalFields, response, e const authKeyName = `datasquirel_${userId}_${database}_auth_key`; const csrfName = `datasquirel_${userId}_${database}_csrf`; - response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${httpResponse.payload.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`]); + response.setHeader("Set-Cookie", [`${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, `${csrfName}=${httpResponse.payload?.csrf_k};samesite=strict;path=/;HttpOnly=true`, `dsqluid=${userId};samesite=strict;path=/;HttpOnly=true`]); } /** ********************************************** */ diff --git a/users/logout-user.js b/users/logout-user.js index 1a26c34..b945ac9 100644 --- a/users/logout-user.js +++ b/users/logout-user.js @@ -1,11 +1,14 @@ +// @ts-check + +const http = require("http"); const parseCookies = require("../utils/functions/parseCookies"); /** * Logout user * ============================================================================== * @param {object} params - Single Param object containing params - * @param {object} params.request - Http request object - * @param {object} params.response - Http response object + * @param {http.IncomingMessage} params.request - Http request object + * @param {http.ServerResponse} params.response - Http response object * @param {string} [params.database] - Target database name(slug): optional => If you don't * include this you will be logged out of all datasquirel websites instead of just the target * database diff --git a/users/reauth-user.js b/users/reauth-user.js index d7242d5..ece21db 100644 --- a/users/reauth-user.js +++ b/users/reauth-user.js @@ -1,8 +1,11 @@ +// @ts-check + /** * ============================================================================== * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); const encrypt = require("../functions/encrypt"); @@ -18,24 +21,7 @@ const userAuth = require("./user-auth"); /** * @typedef {object} FunctionReturn * @property {boolean} success - Did the function run successfully? - * @property {{ - * id: number, - * first_name: string, - * last_name: string, - * username: string, - * email: string, - * phone: string, - * social_id: [string], - * image: string, - * image_thumbnail: string, - * verification_status: [number=0], - * social_login: [number], - * social_platform: [string], - * csrf_k: string, - * more_data: [string], - * logged_in_status: boolean, - * date: string, - * }} payload - Payload + * @property {import("../types/user.td").DATASQUIREL_LoggedInUser | null} payload - Payload * @property {string} [msg] - Response Message * @property {number} [userId] - user ID */ @@ -49,8 +35,8 @@ const userAuth = require("./user-auth"); * @param {object} params - Single Param object containing params * @param {String} params.key - API Key * @param {String} params.database - Target Database - * @param {Object} params.response - Http response object - * @param {Object} params.request - Http request object + * @param {http.ServerResponse} params.response - Http response object + * @param {http.IncomingMessage} params.request - Http request object * @param {string} params.level - Authentication level * @param {String} params.encryptionKey - Encryption Key * @param {String} params.encryptionSalt - Encryption Salt diff --git a/users/social/github-auth.js b/users/social/github-auth.js index 1e27be6..f76a7cb 100644 --- a/users/social/github-auth.js +++ b/users/social/github-auth.js @@ -5,6 +5,7 @@ * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); const encrypt = require("../../functions/encrypt"); @@ -36,7 +37,7 @@ const encrypt = require("../../functions/encrypt"); * @param {string} params.database - Target database name(slug) * @param {string} params.clientId - Github client id * @param {string} params.clientSecret - Github client Secret - * @param {object} params.response - HTTPS response object + * @param {http.ServerResponse} params.response - HTTPS response object * @param {string} params.encryptionKey - Encryption key * @param {string} params.encryptionSalt - Encryption salt * @param {object} [params.additionalFields] - Additional Fields to be added to the user object diff --git a/users/social/google-auth.js b/users/social/google-auth.js index 58f7759..520d48d 100644 --- a/users/social/google-auth.js +++ b/users/social/google-auth.js @@ -1,8 +1,11 @@ +// @ts-check + /** * ============================================================================== * Imports * ============================================================================== */ +const http = require("http"); const https = require("https"); const encrypt = require("../../functions/encrypt"); @@ -14,10 +17,10 @@ const encrypt = require("../../functions/encrypt"); /** ****************************************************************************** */ /** - * @typedef {object} FunctionReturn + * @typedef {object | null} FunctionReturn * @property {boolean} success - Did the function run successfully? - * @property {{id: number, first_name: string, last_name: string}|null} user - Returned User - * @property {number} dsqlUserId - Dsql User Id + * @property {import("../../types/user.td").DATASQUIREL_LoggedInUser | null} user - Returned User + * @property {number} [dsqlUserId] - Dsql User Id * @property {string} [msg] - Response message */ @@ -32,7 +35,7 @@ const encrypt = require("../../functions/encrypt"); * @param {string} params.token - Google access token gotten from the client side * @param {string} params.database - Target database name(slug) * @param {string} params.clientId - Google client id - * @param {object} params.response - HTTPS response object + * @param {http.ServerResponse} params.response - HTTPS response object * @param {string} params.encryptionKey - Encryption key * @param {string} params.encryptionSalt - Encryption salt * @param {object} [params.additionalFields] - Additional Fields to be added to the user object @@ -109,7 +112,7 @@ async function googleAuth({ key, token, database, clientId, response, encryption * Make https request * * @description make a request to datasquirel.com - * @type {{ success: boolean, user: {id: number} | null, msg: string|null } | null} - Https response object + * @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({ diff --git a/users/user-auth.js b/users/user-auth.js index 8b71c88..a2f0032 100644 --- a/users/user-auth.js +++ b/users/user-auth.js @@ -1,3 +1,5 @@ +// @ts-check + /** * ============================================================================== * Imports @@ -16,24 +18,7 @@ const parseCookies = require("../utils/functions/parseCookies"); /** * @typedef {object} AuthenticatedUserObject * @property {boolean} success - Did the function run successfully? - * @property {{ - * id: number, - * first_name: string, - * last_name: string, - * username: string, - * email: string, - * phone: string, - * social_id: [string], - * image: string, - * image_thumbnail: string, - * verification_status: [number=0], - * social_login: [number], - * social_platform: [string], - * csrf_k: string, - * more_data: [string], - * logged_in_status: boolean, - * date: string, - * }} payload - Payload + * @property {import("../types/user.td").DATASQUIREL_LoggedInUser | null} payload - Payload * @property {string} [msg] - Response Message */ diff --git a/utils/functions/parseCookies.js b/utils/functions/parseCookies.js index fb7cea4..39b2593 100644 --- a/utils/functions/parseCookies.js +++ b/utils/functions/parseCookies.js @@ -15,7 +15,7 @@ * @param {object} params - main params object * @param {object} params.request - HTTPS request object * - * @returns {{}|null} + * @returns {object|null} */ module.exports = function ({ request }) { /**