import fs from 'fs-extra';
import path from 'path';
import os from 'os';
const CONFIG_DIR = path.join(os.homedir(), '.overleaf-mcp');
const CONFIG_FILE = path.join(CONFIG_DIR, 'config.json');
export interface AuthConfig {
email?: string;
token?: string;
}
export class AuthManager {
constructor() {
this.ensureConfigDir();
}
private async ensureConfigDir() {
await fs.ensureDir(CONFIG_DIR);
}
async saveConfig(config: AuthConfig) {
const current = await this.getConfig();
const newConfig = { ...current, ...config };
await fs.writeJson(CONFIG_FILE, newConfig, { spaces: 2 });
}
async getConfig(): Promise<AuthConfig> {
if (await fs.pathExists(CONFIG_FILE)) {
return await fs.readJson(CONFIG_FILE);
}
return {};
}
async getCredentials(): Promise<{ email: string; token: string } | null> {
// Check environment variables first
const envEmail = process.env.OVERLEAF_EMAIL;
const envToken = process.env.OVERLEAF_TOKEN;
if (envEmail && envToken) {
return { email: envEmail, token: envToken };
}
// Fallback to config file
const config = await this.getConfig();
if (config.email && config.token) {
return { email: config.email, token: config.token };
}
return null;
}
}