Skip to content
Merged
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
38 changes: 30 additions & 8 deletions packages/void/src/cli/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,38 @@ function page(title: string, message: string): string {
</head><body><div class="card"><h1>${title}</h1><p>${message}</p></div></body></html>`;
}

function openBrowser(url: string, exec: typeof import('node:child_process').exec): void {
if (process.platform === 'win32') {
// `start` treats the first quoted arg as window title, pass empty title
exec(`start "" "${url}"`);
return;
}
if (process.platform === 'darwin') {
exec(`open "${url}"`);
return;
}
// Linux — including WSL2 where `xdg-open` typically has no usable handler.
// Try `wslview` (wslu), then `cmd.exe /c start`, then fall back to `xdg-open`.
if (process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP) {
exec(`wslview "${url}" 2>/dev/null || cmd.exe /c start "" "${url}" 2>/dev/null`);
return;
}
exec(`xdg-open "${url}"`);
}

/**
* Start a local HTTP server, open the browser to the platform login page,
* and wait for the OAuth callback with a JWT token.
*
* `onUrl` is invoked with the login URL once the local server is listening,
* so callers can show the URL to the user as a manual fallback when the
* browser fails to open (common on WSL2, headless SSH, Docker, etc.).
*/
export async function runLogin(root: string = process.cwd(), provider = 'github'): Promise<string> {
export async function runLogin(
root: string = process.cwd(),
provider = 'github',
onUrl?: (url: string) => void,
): Promise<string> {
const http = await import('node:http');
const { exec } = await import('node:child_process');

Expand Down Expand Up @@ -239,13 +266,8 @@ export async function runLogin(root: string = process.cwd(), provider = 'github'
const redirect = `http://localhost:${port}/callback`;
const loginUrl = `${apiUrl}/auth/login?redirect=${encodeURIComponent(redirect)}&provider=${provider}`;

// Open browser with platform detection
if (process.platform === 'win32') {
// `start` treats the first quoted arg as window title, pass empty title
exec(`start "" "${loginUrl}"`);
} else {
exec(`${process.platform === 'darwin' ? 'open' : 'xdg-open'} "${loginUrl}"`);
}
onUrl?.(loginUrl);
openBrowser(loginUrl, exec);
});

// 5-minute timeout
Expand Down
8 changes: 6 additions & 2 deletions packages/void/src/cli/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isStagingMode,
} from './auth.ts';
import { isExpiredTokenError, PlatformClient } from './client.ts';
import { cliTitle, createSpinner } from './output.ts';
import { cliTitle, createSpinner, pc } from './output.ts';

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

try {
const token = await runLogin(root, provider);
const token = await runLogin(root, provider, (url) => {
s.message(
`Waiting for ${providerName} login. If your browser didn't open, visit:\n${pc.cyan(url)}`,
);
});
saveToken(token, root);
s.stop('Logged in!');
return token;
Expand Down
2 changes: 1 addition & 1 deletion packages/void/test/unit/init.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ export default config;
expect(mockConfirm.mock.calls[1]?.[0]).toMatchObject({
message: 'Set up your Void project now? (log in + link project)',
});
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github');
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function));
expect(mockSaveToken).toHaveBeenCalledWith('token_123', root);
expect(mockListProjects).toHaveBeenCalledOnce();
expect(mockCreateProject).toHaveBeenCalledWith('my-live-app');
Expand Down
4 changes: 2 additions & 2 deletions packages/void/test/unit/login-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ describe('runLoginCommand', () => {

expect(mockRemoveToken).toHaveBeenCalledTimes(1);
expect(mockWarn).toHaveBeenCalledWith('Stored auth token expired. Starting a new login.');
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github');
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function));
expect(mockSaveToken).toHaveBeenCalledWith('fresh-token', root);
expect(mockSpinnerStop).toHaveBeenCalledWith('Logged in!');
});
Expand All @@ -128,7 +128,7 @@ describe('runLoginCommand', () => {
expect(mockWarn).toHaveBeenCalledWith(
'`VOID_TOKEN` is invalid or expired. Continuing with a new login.',
);
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github');
expect(mockRunLogin).toHaveBeenCalledWith(root, 'github', expect.any(Function));
expect(mockSaveToken).toHaveBeenCalledWith('fresh-token', root);
expect(mockOutro).toHaveBeenCalledWith('Token saved. Unset `VOID_TOKEN` to use it.');
});
Expand Down