Roll Back compile version
This commit is contained in:
+46
-33
@@ -1,26 +1,33 @@
|
||||
import fs from "fs";
|
||||
import path from "path";
|
||||
import EJSON from "../../../utils/ejson";
|
||||
import debugLog from "../../../utils/logging/debug-log";
|
||||
"use strict";
|
||||
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.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 debug_log_1 = __importDefault(require("../../../utils/logging/debug-log"));
|
||||
function debugFn(log, label) {
|
||||
debugLog({ log, addTime: true, title: "write-auth-files", label });
|
||||
(0, debug_log_1.default)({ log, addTime: true, title: "write-auth-files", label });
|
||||
}
|
||||
export const grabAuthDirs = () => {
|
||||
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(/./))
|
||||
? DSQL_AUTH_DIR
|
||||
: path.resolve(process.cwd(), "./.tmp");
|
||||
const AUTH_DIR = path.join(ROOT_DIR, "logins");
|
||||
: path_1.default.resolve(process.cwd(), "./.tmp");
|
||||
const AUTH_DIR = path_1.default.join(ROOT_DIR, "logins");
|
||||
return { root: ROOT_DIR, auth: AUTH_DIR };
|
||||
};
|
||||
export const initAuthFiles = () => {
|
||||
exports.grabAuthDirs = grabAuthDirs;
|
||||
const initAuthFiles = () => {
|
||||
var _a;
|
||||
try {
|
||||
const authDirs = grabAuthDirs();
|
||||
if (!fs.existsSync(authDirs.root))
|
||||
fs.mkdirSync(authDirs.root, { recursive: true });
|
||||
if (!fs.existsSync(authDirs.auth))
|
||||
fs.mkdirSync(authDirs.auth, { recursive: true });
|
||||
const authDirs = (0, exports.grabAuthDirs)();
|
||||
if (!fs_1.default.existsSync(authDirs.root))
|
||||
fs_1.default.mkdirSync(authDirs.root, { recursive: true });
|
||||
if (!fs_1.default.existsSync(authDirs.auth))
|
||||
fs_1.default.mkdirSync(authDirs.auth, { recursive: true });
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
@@ -29,17 +36,18 @@ export const initAuthFiles = () => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
exports.initAuthFiles = initAuthFiles;
|
||||
/**
|
||||
* # Write Auth Files
|
||||
*/
|
||||
export const writeAuthFile = (name, data, cleanup) => {
|
||||
initAuthFiles();
|
||||
const writeAuthFile = (name, data, cleanup) => {
|
||||
(0, exports.initAuthFiles)();
|
||||
try {
|
||||
const { auth } = grabAuthDirs();
|
||||
const { auth } = (0, exports.grabAuthDirs)();
|
||||
if (cleanup) {
|
||||
cleanupUserAuthFiles(cleanup.userId);
|
||||
(0, exports.cleanupUserAuthFiles)(cleanup.userId);
|
||||
}
|
||||
fs.writeFileSync(path.join(auth, name), data);
|
||||
fs_1.default.writeFileSync(path_1.default.join(auth, name), data);
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
@@ -47,21 +55,22 @@ export const writeAuthFile = (name, data, cleanup) => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
exports.writeAuthFile = writeAuthFile;
|
||||
/**
|
||||
* # Clean up User Auth Files
|
||||
*/
|
||||
export const cleanupUserAuthFiles = (userId) => {
|
||||
initAuthFiles();
|
||||
const cleanupUserAuthFiles = (userId) => {
|
||||
(0, exports.initAuthFiles)();
|
||||
try {
|
||||
const { auth } = grabAuthDirs();
|
||||
const loginFiles = fs.readdirSync(auth);
|
||||
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.join(auth, loginFile);
|
||||
const loginFilePath = path_1.default.join(auth, loginFile);
|
||||
try {
|
||||
const authPayload = EJSON.parse(fs.readFileSync(loginFilePath, "utf-8"));
|
||||
const authPayload = ejson_1.default.parse(fs_1.default.readFileSync(loginFilePath, "utf-8"));
|
||||
if (authPayload.id == userId) {
|
||||
fs.unlinkSync(loginFilePath);
|
||||
fs_1.default.unlinkSync(loginFilePath);
|
||||
}
|
||||
}
|
||||
catch (error) { }
|
||||
@@ -73,39 +82,42 @@ export const cleanupUserAuthFiles = (userId) => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
exports.cleanupUserAuthFiles = cleanupUserAuthFiles;
|
||||
/**
|
||||
* # Get Auth Files
|
||||
*/
|
||||
export const getAuthFile = (name) => {
|
||||
const getAuthFile = (name) => {
|
||||
try {
|
||||
const authFilePath = path.join(grabAuthDirs().auth, name);
|
||||
return fs.readFileSync(authFilePath, "utf-8");
|
||||
const authFilePath = path_1.default.join((0, exports.grabAuthDirs)().auth, name);
|
||||
return fs_1.default.readFileSync(authFilePath, "utf-8");
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error getting Auth File: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
exports.getAuthFile = getAuthFile;
|
||||
/**
|
||||
* # Delete Auth Files
|
||||
* @param {string} name
|
||||
*/
|
||||
export const deleteAuthFile = (name) => {
|
||||
const deleteAuthFile = (name) => {
|
||||
try {
|
||||
return fs.rmSync(path.join(grabAuthDirs().auth, name));
|
||||
return fs_1.default.rmSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name));
|
||||
}
|
||||
catch (error) {
|
||||
console.log(`Error deleting Auth File: ${error.message}`);
|
||||
return null;
|
||||
}
|
||||
};
|
||||
exports.deleteAuthFile = deleteAuthFile;
|
||||
/**
|
||||
* # Delete Auth Files
|
||||
* @param {string} name
|
||||
*/
|
||||
export const checkAuthFile = (name) => {
|
||||
const checkAuthFile = (name) => {
|
||||
try {
|
||||
return fs.existsSync(path.join(grabAuthDirs().auth, name));
|
||||
return fs_1.default.existsSync(path_1.default.join((0, exports.grabAuthDirs)().auth, name));
|
||||
return true;
|
||||
}
|
||||
catch (error) {
|
||||
@@ -113,3 +125,4 @@ export const checkAuthFile = (name) => {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
exports.checkAuthFile = checkAuthFile;
|
||||
|
||||
Reference in New Issue
Block a user