This commit is contained in:
Benjamin Toby
2025-03-17 08:48:58 +01:00
parent ec4d0428bb
commit 34f843bc84
20 changed files with 113 additions and 46 deletions
@@ -2,6 +2,11 @@ import fs from "fs";
import path from "path";
import EJSON from "../../../utils/ejson";
import { DATASQUIREL_LoggedInUser } from "../../../types";
import debugLog from "../../../utils/logging/debug-log";
function debugFn(log: any, label?: string) {
debugLog({ log, addTime: true, title: "write-auth-files", label });
}
export const grabAuthDirs = () => {
const DSQL_AUTH_DIR = process.env.DSQL_AUTH_DIR;
@@ -43,14 +48,15 @@ export const writeAuthFile = (
}
) => {
initAuthFiles();
try {
const { auth, root } = grabAuthDirs();
const { auth } = grabAuthDirs();
if (cleanup) {
cleanupUserAuthFiles(cleanup.userId);
}
fs.writeFileSync(path.join(auth, name), data);
return true;
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error writing Auth File: ${error.message}`);
return false;
}
@@ -67,14 +73,16 @@ export const cleanupUserAuthFiles = (userId: string | number) => {
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) {}
} catch (error: any) {}
}
return true;
} catch (error: any) {
@@ -90,7 +98,7 @@ export const getAuthFile = (name: string) => {
try {
const authFilePath = path.join(grabAuthDirs().auth, name);
return fs.readFileSync(authFilePath, "utf-8");
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error getting Auth File: ${error.message}`);
return null;
}
@@ -103,7 +111,7 @@ export const getAuthFile = (name: string) => {
export const deleteAuthFile = (name: string) => {
try {
return fs.rmSync(path.join(grabAuthDirs().auth, name));
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error deleting Auth File: ${error.message}`);
return null;
}
@@ -117,7 +125,7 @@ export const checkAuthFile = (name: string) => {
try {
return fs.existsSync(path.join(grabAuthDirs().auth, name));
return true;
} catch (/** @type {any} */ error: any) {
} catch (error: any) {
console.log(`Error checking Auth File: ${error.message}`);
return false;
}