60 lines
1.3 KiB
TypeScript
60 lines
1.3 KiB
TypeScript
export interface LoginConfig {
|
|
serverUrl: string;
|
|
email: string;
|
|
username: string;
|
|
domain?: string;
|
|
}
|
|
|
|
import fs from "node:fs";
|
|
import path from "node:path";
|
|
|
|
let dataDir: string = "./data";
|
|
|
|
export function initDataDir(dir: string): void {
|
|
dataDir = path.resolve(dir);
|
|
fs.mkdirSync(dataDir, { recursive: true });
|
|
}
|
|
|
|
let inMemoryPassword: string | null = null;
|
|
|
|
export function setPassword(password: string): void {
|
|
inMemoryPassword = password;
|
|
}
|
|
|
|
export function getPassword(): string | null {
|
|
return inMemoryPassword;
|
|
}
|
|
|
|
export function hasPassword(): boolean {
|
|
return inMemoryPassword !== null;
|
|
}
|
|
|
|
export function clearPassword(): void {
|
|
inMemoryPassword = null;
|
|
}
|
|
|
|
function configFilePath(): string {
|
|
return path.join(dataDir, "config.json");
|
|
}
|
|
|
|
export function loadConfig(): LoginConfig {
|
|
const filePath = configFilePath();
|
|
if (!fs.existsSync(filePath)) {
|
|
throw new Error("Not logged in. Use the login tool first.");
|
|
}
|
|
return JSON.parse(fs.readFileSync(filePath, "utf-8")) as LoginConfig;
|
|
}
|
|
|
|
export function saveConfig(config: LoginConfig): void {
|
|
const filePath = configFilePath();
|
|
fs.writeFileSync(filePath, JSON.stringify(config, null, 2), "utf-8");
|
|
}
|
|
|
|
export function clearConfig(): void {
|
|
const filePath = configFilePath();
|
|
if (fs.existsSync(filePath)) {
|
|
fs.unlinkSync(filePath);
|
|
}
|
|
clearPassword();
|
|
}
|