Skip to content

Commit 39ce42b

Browse files
authored
Studio Code: Display login URL to improve experience in remote and headless environments (#3900)
- **Always prints the authorization URL explicitly**, as a clean copyable line, whether or not a browser opens — so users on any machine can open it on another device and paste back the token. - Downgrades a failed browser launch from a scary error to a friendly warning that points at the printed URL.
1 parent 3d898ae commit 39ce42b

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎apps/cli/commands/auth/login.ts‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AUTH_EVENTS } from '@studio/common/lib/cli-events';
44
import { getAuthenticationUrl } from '@studio/common/lib/oauth';
55
import { readAuthToken, updateSharedConfig } from '@studio/common/lib/shared-config';
66
import { AuthCommandLoggerAction as LoggerAction } from '@studio/common/logger-actions';
7-
import { __, sprintf } from '@wordpress/i18n';
7+
import { __ } from '@wordpress/i18n';
88
import { getUserInfo } from 'cli/lib/api';
99
import { openBrowser } from 'cli/lib/browser';
1010
import { emitCliEvent } from 'cli/lib/daemon-client';
@@ -30,26 +30,27 @@ export async function runCommand(): Promise< void > {
3030
return;
3131
}
3232

33-
logger.reportStart( LoggerAction.LOGIN, __( 'Opening browser for authentication…' ) );
34-
3533
const appLocale = await getAppLocale();
3634
const authUrl = getAuthenticationUrl( appLocale, CLI_REDIRECT_URI );
3735

36+
logger.reportStart( LoggerAction.LOGIN, __( 'Opening browser for authentication…' ) );
3837
try {
3938
await openBrowser( authUrl );
4039
logger.reportSuccess( __( 'Browser opened successfully' ) );
4140
} catch ( error ) {
42-
// If the browser fails to open, allow users to manually open the URL
43-
const loggerError = new LoggerError(
44-
sprintf( __( 'Failed to open browser. Please open the URL manually: %s' ), authUrl ),
45-
error
41+
logger.reportWarning(
42+
__( "Couldn't open a browser automatically. Use the URL below instead." ) +
43+
( error instanceof Error ? `: ${ error.message }` : '' )
4644
);
47-
logger.reportError( loggerError );
4845
}
4946

50-
console.log(
51-
__( 'Please complete authentication in your browser and paste the generated token here.' )
52-
);
47+
// Always surface the URL explicitly so users on remote/headless machines
48+
// have a usable link to open on another device.
49+
console.log( '' );
50+
console.log( __( 'To authenticate, open this URL in a browser on any device:' ) );
51+
console.log( authUrl );
52+
console.log( '' );
53+
console.log( __( 'After approving access, copy the generated token and paste it here.' ) );
5354
console.log( '' );
5455

5556
let accessToken: Awaited< ReturnType< typeof input > >;

‎apps/cli/commands/auth/tests/login.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ describe( 'Auth Login Command', () => {
147147
expect( mockReportError ).toHaveBeenCalledWith( expect.any( LoggerError ) );
148148
} );
149149

150+
it( 'should print the URL even when the browser opens', async () => {
151+
const logSpy = vi.spyOn( console, 'log' ).mockImplementation( () => undefined );
152+
153+
await runCommand();
154+
155+
expect( openBrowser ).toHaveBeenCalledWith( mockAuthUrl );
156+
expect( logSpy ).toHaveBeenCalledWith( mockAuthUrl );
157+
158+
logSpy.mockRestore();
159+
} );
160+
150161
it( 'should use provided locale', async () => {
151162
vi.mocked( getAppLocale ).mockResolvedValue( 'fr' );
152163

0 commit comments

Comments
 (0)