Skip to content

Commit 88d117c

Browse files
authored
fix(cli): show login URL and support WSL2 browser opening (#186)
1 parent f3e7932 commit 88d117c

4 files changed

Lines changed: 39 additions & 13 deletions

File tree

‎packages/void/src/cli/auth.ts‎

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

‎packages/void/src/cli/login.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
isStagingMode,
99
} from './auth.ts';
1010
import { isExpiredTokenError, PlatformClient } from './client.ts';
11-
import { cliTitle, createSpinner } from './output.ts';
11+
import { cliTitle, createSpinner, pc } from './output.ts';
1212

1313
export async function promptForLoginToken(root: string = process.cwd()): Promise<string> {
1414
const provider = await select({
@@ -29,7 +29,11 @@ export async function promptForLoginToken(root: string = process.cwd()): Promise
2929
s.start(`Opening browser for ${providerName} login...`);
3030

3131
try {
32-
const token = await runLogin(root, provider);
32+
const token = await runLogin(root, provider, (url) => {
33+
s.message(
34+
`Waiting for ${providerName} login. If your browser didn't open, visit:\n${pc.cyan(url)}`,
35+
);
36+
});
3337
saveToken(token, root);
3438
s.stop('Logged in!');
3539
return token;

‎packages/void/test/unit/init.test.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1908,7 +1908,7 @@ export default config;
19081908
expect(mockConfirm.mock.calls[1]?.[0]).toMatchObject({
19091909
message: 'Set up your Void project now? (log in + link project)',
19101910
});
1911-
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github');
1911+
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function));
19121912
expect(mockSaveToken).toHaveBeenCalledWith('token_123', root);
19131913
expect(mockListProjects).toHaveBeenCalledOnce();
19141914
expect(mockCreateProject).toHaveBeenCalledWith('my-live-app');

‎packages/void/test/unit/login-command.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ describe('runLoginCommand', () => {
111111

112112
expect(mockRemoveToken).toHaveBeenCalledTimes(1);
113113
expect(mockWarn).toHaveBeenCalledWith('Stored auth token expired. Starting a new login.');
114-
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github');
114+
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function));
115115
expect(mockSaveToken).toHaveBeenCalledWith('fresh-token', root);
116116
expect(mockSpinnerStop).toHaveBeenCalledWith('Logged in!');
117117
});
@@ -128,7 +128,7 @@ describe('runLoginCommand', () => {
128128
expect(mockWarn).toHaveBeenCalledWith(
129129
'`VOID_TOKEN` is invalid or expired. Continuing with a new login.',
130130
);
131-
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github');
131+
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function));
132132
expect(mockSaveToken).toHaveBeenCalledWith('fresh-token', root);
133133
expect(mockOutro).toHaveBeenCalledWith('Token saved. Unset `VOID_TOKEN` to use it.');
134134
});

0 commit comments

Comments
 (0)