52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import fs from "node:fs";
|
|
import { configFilePath, initDataDir } from "./data.ts";
|
|
|
|
// Re-export for backward compatibility
|
|
export { initDataDir };
|
|
|
|
export interface LoginConfig {
|
|
serverUrl: string;
|
|
email: string;
|
|
username: string;
|
|
domain?: string;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
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();
|
|
}
|