@@ -176,11 +176,38 @@ function page(title: string, message: string): string {
176176</head><body><div class="card"><h1>${ title } </h1><p>${ message } </p></div></body></html>` ;
177177}
178178
179+ function openBrowser ( url : string , exec : typeof import ( 'node:child_process' ) . exec ) : void {
180+ if ( process . platform === 'win32' ) {
181+ // `start` treats the first quoted arg as window title, pass empty title
182+ exec ( `start "" "${ url } "` ) ;
183+ return ;
184+ }
185+ if ( process . platform === 'darwin' ) {
186+ exec ( `open "${ url } "` ) ;
187+ return ;
188+ }
189+ // Linux — including WSL2 where `xdg-open` typically has no usable handler.
190+ // Try `wslview` (wslu), then `cmd.exe /c start`, then fall back to `xdg-open`.
191+ if ( process . env . WSL_DISTRO_NAME || process . env . WSL_INTEROP ) {
192+ exec ( `wslview "${ url } " 2>/dev/null || cmd.exe /c start "" "${ url } " 2>/dev/null` ) ;
193+ return ;
194+ }
195+ exec ( `xdg-open "${ url } "` ) ;
196+ }
197+
179198/**
180199 * Start a local HTTP server, open the browser to the platform login page,
181200 * and wait for the OAuth callback with a JWT token.
201+ *
202+ * `onUrl` is invoked with the login URL once the local server is listening,
203+ * so callers can show the URL to the user as a manual fallback when the
204+ * browser fails to open (common on WSL2, headless SSH, Docker, etc.).
182205 */
183- export async function runLogin ( root : string = process . cwd ( ) , provider = 'github' ) : Promise < string > {
206+ export async function runLogin (
207+ root : string = process . cwd ( ) ,
208+ provider = 'github' ,
209+ onUrl ?: ( url : string ) => void ,
210+ ) : Promise < string > {
184211 const http = await import ( 'node:http' ) ;
185212 const { exec } = await import ( 'node:child_process' ) ;
186213
@@ -239,13 +266,8 @@ export async function runLogin(root: string = process.cwd(), provider = 'github'
239266 const redirect = `http://localhost:${ port } /callback` ;
240267 const loginUrl = `${ apiUrl } /auth/login?redirect=${ encodeURIComponent ( redirect ) } &provider=${ provider } ` ;
241268
242- // Open browser with platform detection
243- if ( process . platform === 'win32' ) {
244- // `start` treats the first quoted arg as window title, pass empty title
245- exec ( `start "" "${ loginUrl } "` ) ;
246- } else {
247- exec ( `${ process . platform === 'darwin' ? 'open' : 'xdg-open' } "${ loginUrl } "` ) ;
248- }
269+ onUrl ?.( loginUrl ) ;
270+ openBrowser ( loginUrl , exec ) ;
249271 } ) ;
250272
251273 // 5-minute timeout
0 commit comments