diff --git a/dist/package-shared/actions/users/login-user.d.ts b/dist/package-shared/actions/users/login-user.d.ts index ec465e0..b593ac5 100644 --- a/dist/package-shared/actions/users/login-user.d.ts +++ b/dist/package-shared/actions/users/login-user.d.ts @@ -24,9 +24,10 @@ type Param = { skipWriteAuthFile?: boolean; apiUserID?: string | number; dbUserId?: string | number; + cleanupTokens?: boolean; }; /** * # Login A user */ -export default function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, apiUserID, skipWriteAuthFile, dbUserId, debug, }: Param): Promise; +export default function loginUser({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, apiUserID, skipWriteAuthFile, dbUserId, debug, cleanupTokens, }: Param): Promise; export {}; diff --git a/dist/package-shared/actions/users/login-user.js b/dist/package-shared/actions/users/login-user.js index c3e7802..8d7ec23 100644 --- a/dist/package-shared/actions/users/login-user.js +++ b/dist/package-shared/actions/users/login-user.js @@ -25,8 +25,8 @@ const debug_log_1 = __importDefault(require("../../utils/logging/debug-log")); * # Login A user */ function loginUser(_a) { - return __awaiter(this, arguments, void 0, function* ({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, apiUserID, skipWriteAuthFile, dbUserId, debug, }) { - var _b, _c; + return __awaiter(this, arguments, void 0, function* ({ key, payload, database, additionalFields, response, encryptionKey, encryptionSalt, email_login, email_login_code, temp_code_field, token, user_id, skipPassword, apiUserID, skipWriteAuthFile, dbUserId, debug, cleanupTokens, }) { + var _b, _c, _d; const grabedHostNames = (0, grab_host_names_1.default)({ userId: user_id || apiUserID }); const { host, port, scheme } = grabedHostNames; const defaultTempLoginFieldName = "temp_login_code"; @@ -172,7 +172,9 @@ function loginUser(_a) { userId: grabedHostNames.user_id, }); if (httpResponse.csrf && !skipWriteAuthFile) { - (0, write_auth_files_1.writeAuthFile)(httpResponse.csrf, JSON.stringify(httpResponse.payload)); + (0, write_auth_files_1.writeAuthFile)(httpResponse.csrf, JSON.stringify(httpResponse.payload), cleanupTokens && ((_c = httpResponse.payload) === null || _c === void 0 ? void 0 : _c.id) + ? { userId: httpResponse.payload.id } + : undefined); } httpResponse["cookieNames"] = cookieNames; httpResponse["key"] = String(encryptedPayload); @@ -185,7 +187,7 @@ function loginUser(_a) { } response === null || response === void 0 ? void 0 : response.setHeader("Set-Cookie", [ `${authKeyName}=${encryptedPayload};samesite=strict;path=/;HttpOnly=true;Secure=true`, - `${csrfName}=${(_c = httpResponse.payload) === null || _c === void 0 ? void 0 : _c.csrf_k};samesite=strict;path=/;HttpOnly=true`, + `${csrfName}=${(_d = httpResponse.payload) === null || _d === void 0 ? void 0 : _d.csrf_k};samesite=strict;path=/;HttpOnly=true`, ]); if (debug) { debugFn("Response Sent!"); diff --git a/dist/package-shared/functions/backend/auth/write-auth-files.d.ts b/dist/package-shared/functions/backend/auth/write-auth-files.d.ts index 1d78968..fd55e29 100644 --- a/dist/package-shared/functions/backend/auth/write-auth-files.d.ts +++ b/dist/package-shared/functions/backend/auth/write-auth-files.d.ts @@ -6,7 +6,13 @@ export declare const initAuthFiles: () => boolean; /** * # Write Auth Files */ -export declare const writeAuthFile: (name: string, data: string) => boolean; +export declare const writeAuthFile: (name: string, data: string, cleanup?: { + userId: string | number; +}) => boolean; +/** + * # Clean up User Auth Files + */ +export declare const cleanupUserAuthFiles: (userId: string | number) => boolean; /** * # Get Auth Files */ diff --git a/dist/package-shared/functions/backend/auth/write-auth-files.js b/dist/package-shared/functions/backend/auth/write-auth-files.js index 2060b28..0d23a60 100644 --- a/dist/package-shared/functions/backend/auth/write-auth-files.js +++ b/dist/package-shared/functions/backend/auth/write-auth-files.js @@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -exports.checkAuthFile = exports.deleteAuthFile = exports.getAuthFile = exports.writeAuthFile = exports.initAuthFiles = exports.grabAuthDirs = void 0; +exports.checkAuthFile = exports.deleteAuthFile = exports.getAuthFile = exports.cleanupUserAuthFiles = exports.writeAuthFile = exports.initAuthFiles = exports.grabAuthDirs = void 0; const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); +const ejson_1 = __importDefault(require("../../../utils/ejson")); const grabAuthDirs = () => { const DSQL_AUTH_DIR = process.env.DSQL_AUTH_DIR; const ROOT_DIR = (DSQL_AUTH_DIR === null || DSQL_AUTH_DIR === void 0 ? void 0 : DSQL_AUTH_DIR.match(/./)) @@ -35,10 +36,14 @@ exports.initAuthFiles = initAuthFiles; /** * # Write Auth Files */ -const writeAuthFile = (name, data) => { +const writeAuthFile = (name, data, cleanup) => { (0, exports.initAuthFiles)(); try { - fs_1.default.writeFileSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name), data); + const { auth, root } = (0, exports.grabAuthDirs)(); + if (cleanup) { + (0, exports.cleanupUserAuthFiles)(cleanup.userId); + } + fs_1.default.writeFileSync(path_1.default.join(auth, name), data); return true; } catch ( /** @type {any} */error) { @@ -47,6 +52,33 @@ const writeAuthFile = (name, data) => { } }; exports.writeAuthFile = writeAuthFile; +/** + * # Clean up User Auth Files + */ +const cleanupUserAuthFiles = (userId) => { + (0, exports.initAuthFiles)(); + try { + const { auth } = (0, exports.grabAuthDirs)(); + const loginFiles = fs_1.default.readdirSync(auth); + for (let i = 0; i < loginFiles.length; i++) { + const loginFile = loginFiles[i]; + const loginFilePath = path_1.default.join(auth, loginFile); + try { + const authPayload = ejson_1.default.parse(fs_1.default.readFileSync(loginFilePath, "utf-8")); + if (authPayload.id == userId) { + fs_1.default.unlinkSync(loginFilePath); + } + } + catch (error) { } + } + return true; + } + catch (error) { + console.log(`Error Cleaning up User Auth Files: ${error.message}`); + return false; + } +}; +exports.cleanupUserAuthFiles = cleanupUserAuthFiles; /** * # Get Auth Files */ diff --git a/package-shared/actions/users/login-user.ts b/package-shared/actions/users/login-user.ts index f8623f6..25594cf 100644 --- a/package-shared/actions/users/login-user.ts +++ b/package-shared/actions/users/login-user.ts @@ -35,6 +35,7 @@ type Param = { skipWriteAuthFile?: boolean; apiUserID?: string | number; dbUserId?: string | number; + cleanupTokens?: boolean; }; /** @@ -58,6 +59,7 @@ export default async function loginUser({ skipWriteAuthFile, dbUserId, debug, + cleanupTokens, }: Param): Promise { const grabedHostNames = grabHostNames({ userId: user_id || apiUserID }); const { host, port, scheme } = grabedHostNames; @@ -244,7 +246,10 @@ export default async function loginUser({ if (httpResponse.csrf && !skipWriteAuthFile) { writeAuthFile( httpResponse.csrf, - JSON.stringify(httpResponse.payload) + JSON.stringify(httpResponse.payload), + cleanupTokens && httpResponse.payload?.id + ? { userId: httpResponse.payload.id } + : undefined ); } diff --git a/package-shared/functions/backend/auth/write-auth-files.ts b/package-shared/functions/backend/auth/write-auth-files.ts index 4ade0da..8d1520b 100644 --- a/package-shared/functions/backend/auth/write-auth-files.ts +++ b/package-shared/functions/backend/auth/write-auth-files.ts @@ -1,5 +1,7 @@ import fs from "fs"; import path from "path"; +import EJSON from "../../../utils/ejson"; +import { DATASQUIREL_LoggedInUser } from "../../../types"; export const grabAuthDirs = () => { const DSQL_AUTH_DIR = process.env.DSQL_AUTH_DIR; @@ -33,10 +35,20 @@ export const initAuthFiles = () => { /** * # Write Auth Files */ -export const writeAuthFile = (name: string, data: string) => { +export const writeAuthFile = ( + name: string, + data: string, + cleanup?: { + userId: string | number; + } +) => { initAuthFiles(); try { - fs.writeFileSync(path.join(grabAuthDirs().auth, name), data); + const { auth, root } = grabAuthDirs(); + if (cleanup) { + cleanupUserAuthFiles(cleanup.userId); + } + fs.writeFileSync(path.join(auth, name), data); return true; } catch (/** @type {any} */ error: any) { console.log(`Error writing Auth File: ${error.message}`); @@ -44,6 +56,33 @@ export const writeAuthFile = (name: string, data: string) => { } }; +/** + * # Clean up User Auth Files + */ +export const cleanupUserAuthFiles = (userId: string | number) => { + initAuthFiles(); + try { + const { auth } = grabAuthDirs(); + const loginFiles = fs.readdirSync(auth); + for (let i = 0; i < loginFiles.length; i++) { + const loginFile = loginFiles[i]; + const loginFilePath = path.join(auth, loginFile); + try { + const authPayload = EJSON.parse( + fs.readFileSync(loginFilePath, "utf-8") + ) as DATASQUIREL_LoggedInUser; + if (authPayload.id == userId) { + fs.unlinkSync(loginFilePath); + } + } catch (error) {} + } + return true; + } catch (error: any) { + console.log(`Error Cleaning up User Auth Files: ${error.message}`); + return false; + } +}; + /** * # Get Auth Files */ diff --git a/package.json b/package.json index 27edefc..beed334 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "4.1.8", + "version": "4.1.9", "description": "Cloud-based SQL data management tool", "main": "dist/index.js", "bin": {