Skip to content

Add fallback to manual URL paste when browser cannot open#66

Merged
jancurn merged 2 commits intomainfrom
claude/headless-oauth-auth-pZejg
Mar 12, 2026
Merged

Add fallback to manual URL paste when browser cannot open#66
jancurn merged 2 commits intomainfrom
claude/headless-oauth-auth-pZejg

Conversation

@jancurn
Copy link
Member

@jancurn jancurn commented Mar 12, 2026

Summary

Enhanced the OAuth flow to gracefully handle environments where a browser cannot be automatically opened (e.g., headless servers, containers). When browser launch fails, users can now manually paste the callback URL instead of the flow failing entirely.

Key Changes

  • Renamed openBrowser() to tryOpenBrowser() and changed it to return a boolean indicating success/failure instead of throwing an error
  • Added extractCodeFromUrl() function to parse authorization codes and state from callback URLs, with proper error handling for malformed URLs and OAuth errors
  • Added promptForCallbackUrl() function to interactively prompt users to paste the callback URL using readline (compatible with all terminal environments)
  • Updated performOAuthFlow() to detect browser launch failures and fall back to URL paste mode
  • Improved user messaging to guide users through the manual flow when browser cannot be opened
  • Added proper cleanup handlers for both escape key and URL paste prompts to prevent resource leaks

Implementation Details

  • The fallback mechanism uses Promise.race() to allow either the callback server or manual URL paste to complete the flow
  • URL parsing handles both full URLs and query strings for flexibility
  • Readline interface is used instead of raw mode to ensure compatibility across different terminal environments
  • Cleanup is properly handled in both success and error paths to avoid hanging processes

https://claude.ai/code/session_01XP5tu7h6nuEAaavYWQab6q

claude added 2 commits March 12, 2026 22:20
On headless Linux environments (e.g. remote servers, containers, CI), the
OAuth login flow fails because there's no browser to open and localhost may
not be reachable from the user's machine. The new --headless flag prints the
authorization URL for the user to copy to any browser, then accepts the
pasted callback URL to extract the authorization code. The callback server
still runs as a fallback in case localhost is reachable.

https://claude.ai/code/session_01XP5tu7h6nuEAaavYWQab6q
… login

Instead of a separate --headless flag, the login flow now automatically
detects when the browser cannot be opened and falls back to prompting the
user to paste the callback URL from their browser. This handles headless
environments (remote servers, containers) without requiring any extra flags.

https://claude.ai/code/session_01XP5tu7h6nuEAaavYWQab6q
@jancurn jancurn merged commit 788161a into main Mar 12, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants