Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ program
setSessionCookie(options.cookie);

if (options.saveLocal) {
saveOlAuth(options.cookie);
saveOlAuth(options.cookie, undefined, baseUrl, cookieName);
spinner.succeed(`Authenticated! Found ${projects.length} projects. Saved to .olauth`);
} else {
spinner.succeed(`Authenticated! Found ${projects.length} projects.`);
Expand Down
62 changes: 57 additions & 5 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,59 @@ const config = new Conf<OlcliConfig>({
});

export function getBaseUrl(): string {
return process.env.OVERLEAF_BASE_URL || config.get('baseUrl') || 'https://www.overleaf.com';
if (process.env.OVERLEAF_BASE_URL) {
return process.env.OVERLEAF_BASE_URL;
}

// Check .olauth file in current directory
const olAuthPath = join(process.cwd(), '.olauth');
if (existsSync(olAuthPath)) {
try {
const content = readFileSync(olAuthPath, 'utf-8').trim();
if (content.includes('=')) {
const parts = content.split(';').map(c => c.trim());
const baseUrlPart = parts.find(c => c.startsWith('baseUrl='));
if (baseUrlPart) {
return baseUrlPart.substring('baseUrl='.length);
}
}
} catch {
// Ignore errors
}
}

return config.get('baseUrl') || 'https://www.overleaf.com';
}

export function setBaseUrl(url: string): void {
config.set('baseUrl', url);
}

export function getSessionCookieName(): string {
return process.env.OVERLEAF_COOKIE_NAME || config.get('sessionCookieName') || 'overleaf_session2';
if (process.env.OVERLEAF_COOKIE_NAME) {
return process.env.OVERLEAF_COOKIE_NAME;
}

// Infer from .olauth file in current directory
const olAuthPath = join(process.cwd(), '.olauth');
if (existsSync(olAuthPath)) {
try {
const content = readFileSync(olAuthPath, 'utf-8').trim();
if (content.includes('=')) {
const parts = content.split(';').map(c => c.trim());
for (const part of parts) {
const [key] = part.split('=');
if (key && key !== 'baseUrl') {
return key;
}
}
}
} catch {
// Ignore errors
}
}

return config.get('sessionCookieName') || 'overleaf_session2';
}

export function setSessionCookieName(name: string): void {
Expand All @@ -59,7 +103,7 @@ export function getSessionCookie(): string | undefined {
const cookieName = getSessionCookieName();
const sessionCookie = cookies.find(c => c.startsWith(`${cookieName}=`));
if (sessionCookie) {
return sessionCookie.split('=')[1];
return sessionCookie.substring(`${cookieName}=`.length);
}
}
return content;
Expand Down Expand Up @@ -103,7 +147,15 @@ export function getConfigPath(): string {
/**
* Save session cookie in .olauth format for compatibility
*/
export function saveOlAuth(cookie: string, path?: string): void {
export function saveOlAuth(cookie: string, path?: string, customBaseUrl?: string, customCookieName?: string): void {
const authPath = path || join(process.cwd(), '.olauth');
writeFileSync(authPath, `${getSessionCookieName()}=${cookie}`, 'utf-8');
const baseUrl = customBaseUrl || getBaseUrl();
const cookieName = customCookieName || getSessionCookieName();

let content = `${cookieName}=${cookie}`;
if (baseUrl && baseUrl !== 'https://www.overleaf.com') {
content += `; baseUrl=${baseUrl}`;
}

writeFileSync(authPath, content, 'utf-8');
}