Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
999b2a8
CLI: Implement `auth login` command
fredrikekelund Nov 5, 2025
8650b02
Fix lint and tests
fredrikekelund Nov 6, 2025
fc661e0
Add tests, tweak command
fredrikekelund Nov 6, 2025
c8b228c
Consider expiration time
fredrikekelund Nov 6, 2025
9fcc45f
Move logic around
fredrikekelund Nov 6, 2025
36e6059
Unwrap
fredrikekelund Nov 6, 2025
a228ae9
Fix type error
fredrikekelund Nov 6, 2025
b838c0c
CLI: Implement `auth logout` command
fredrikekelund Nov 6, 2025
4634320
CLI: Implement `auth status` command
fredrikekelund Nov 6, 2025
c1799fe
Address review comments
fredrikekelund Nov 6, 2025
23aa26c
Reject unsupported platforms
fredrikekelund Nov 6, 2025
ed4f216
Merge branch 'f26d/cli-auth-login-command' into f26d/cli-auth-logout-…
fredrikekelund Nov 6, 2025
a68c998
Merge branch 'f26d/cli-auth-logout-command' into f26d/cli-auth-status…
fredrikekelund Nov 6, 2025
49966e3
Merge branch 'trunk' into f26d/cli-auth-login-command
fredrikekelund Nov 7, 2025
e13fd34
Address review feedback
fredrikekelund Nov 7, 2025
c72a70d
Fix test
fredrikekelund Nov 7, 2025
edb407f
Merge branch 'f26d/cli-auth-login-command' into f26d/cli-auth-logout-…
fredrikekelund Nov 7, 2025
6ba6a05
Fix
fredrikekelund Nov 7, 2025
6946324
Merge branch 'f26d/cli-auth-logout-command' into f26d/cli-auth-status…
fredrikekelund Nov 7, 2025
2908170
Merge branch 'trunk' into f26d/cli-auth-login-command
fredrikekelund Nov 17, 2025
fc816bf
docs: Update CLI documentation for auth login command
github-actions[bot] Nov 17, 2025
5748109
Restore package-lock.json
fredrikekelund Nov 17, 2025
7b1c743
Install inquirer dependency again
fredrikekelund Nov 17, 2025
d8feb6b
Tweak
fredrikekelund Nov 17, 2025
20c7566
Merge branch 'f26d/cli-auth-login-command' into f26d/cli-auth-logout-…
fredrikekelund Nov 17, 2025
fdb06df
Merge branch 'f26d/cli-auth-logout-command' into f26d/cli-auth-status…
fredrikekelund Nov 17, 2025
1cc1fe0
docs: Update CLI documentation for auth commands
github-actions[bot] Nov 17, 2025
9df3824
Merge branch 'trunk' into f26d/cli-auth-status-command
fredrikekelund Nov 17, 2025
3233720
docs: Add CLI documentation for auth status command
github-actions[bot] Nov 17, 2025
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions cli/commands/auth/login.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { password } from '@inquirer/prompts';
import { input } from '@inquirer/prompts';
import { __, sprintf } from '@wordpress/i18n';
import { DEFAULT_TOKEN_LIFETIME_MS } from 'common/constants';
import { getAuthenticationUrl } from 'common/lib/oauth';
import { AuthCommandLoggerAction as LoggerAction } from 'common/logger-actions';
import { getUserInfo } from 'cli/lib/api';
Expand Down Expand Up @@ -50,38 +51,40 @@ export async function runCommand(): Promise< void > {
);
console.log( '' );

try {
const accessToken = await password( { message: __( 'Authentication token:' ) } );
const user = await getUserInfo( accessToken );
let accessToken: Awaited< ReturnType< typeof input > >;
let user: Awaited< ReturnType< typeof getUserInfo > >;

try {
accessToken = await input( { message: __( 'Authentication token:' ) } );
user = await getUserInfo( accessToken );
logger.reportSuccess( __( 'Authentication completed successfully!' ) );
} catch ( error ) {
logger.reportError( new LoggerError( __( 'Authentication failed. Please try again.' ) ) );
return;
}

try {
await lockAppdata();
const userData = await readAppdata();

const now = new Date();
const twoWeeksInSeconds = 2 * 7 * 24 * 60 * 60;
try {
await lockAppdata();
const userData = await readAppdata();

userData.authToken = {
accessToken,
id: user.ID,
email: user.email,
displayName: user.display_name,
expiresIn: twoWeeksInSeconds,
expirationTime: now.getTime() + twoWeeksInSeconds * 1000,
};
userData.authToken = {
accessToken,
id: user.ID,
email: user.email,
displayName: user.display_name,
expiresIn: DEFAULT_TOKEN_LIFETIME_MS / 1000,
expirationTime: Date.now() + DEFAULT_TOKEN_LIFETIME_MS,
};

await saveAppdata( userData );
} finally {
await unlockAppdata();
}
await saveAppdata( userData );
} catch ( error ) {
if ( error instanceof LoggerError ) {
logger.reportError( error );
} else {
logger.reportError( new LoggerError( __( 'Authentication failed' ), error ) );
}
} finally {
await unlockAppdata();
}
}

Expand Down
21 changes: 7 additions & 14 deletions cli/commands/auth/tests/login.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { password } from '@inquirer/prompts';
import { input } from '@inquirer/prompts';
import { getAuthenticationUrl } from 'common/lib/oauth';
import { getUserInfo } from 'cli/lib/api';
import {
Expand All @@ -15,14 +15,7 @@ import { Logger, LoggerError } from 'cli/logger';
jest.mock( '@inquirer/prompts' );
jest.mock( 'common/lib/oauth' );
jest.mock( 'cli/lib/api' );
jest.mock( 'cli/lib/appdata', () => ( {
...jest.requireActual( 'cli/lib/appdata' ),
lockAppdata: jest.fn(),
readAppdata: jest.fn(),
saveAppdata: jest.fn(),
unlockAppdata: jest.fn(),
getAuthToken: jest.fn(),
} ) );
jest.mock( 'cli/lib/appdata' );
jest.mock( 'cli/lib/browser' );
jest.mock( 'cli/lib/i18n' );
jest.mock( 'cli/logger' );
Expand Down Expand Up @@ -68,7 +61,7 @@ describe( 'Auth Login Command', () => {
( getAppLocale as jest.Mock ).mockResolvedValue( 'en' );
( getUserInfo as jest.Mock ).mockResolvedValue( mockUserData );
( openBrowser as jest.Mock ).mockResolvedValue( undefined );
( password as jest.Mock ).mockResolvedValue( mockAccessToken );
( input as jest.Mock ).mockResolvedValue( mockAccessToken );
( readAppdata as jest.Mock ).mockResolvedValue( mockAppdata );
( getAuthToken as jest.Mock ).mockRejectedValue( new Error( 'Mock error' ) );
( lockAppdata as jest.Mock ).mockResolvedValue( undefined );
Expand All @@ -87,7 +80,7 @@ describe( 'Auth Login Command', () => {
await runCommand();

expect( openBrowser ).not.toHaveBeenCalled();
expect( password ).not.toHaveBeenCalled();
expect( input ).not.toHaveBeenCalled();
} );

it( 'should complete the login process successfully', async () => {
Expand All @@ -99,7 +92,7 @@ describe( 'Auth Login Command', () => {
'https://developer.wordpress.com/copy-oauth-token'
);
expect( openBrowser ).toHaveBeenCalledWith( mockAuthUrl );
expect( password ).toHaveBeenCalledWith( {
expect( input ).toHaveBeenCalledWith( {
message: 'Authentication token:',
} );
expect( getUserInfo ).toHaveBeenCalledWith( mockAccessToken );
Expand All @@ -122,7 +115,7 @@ describe( 'Auth Login Command', () => {
await runCommand();

expect( openBrowser ).toHaveBeenCalled();
expect( password ).toHaveBeenCalled();
expect( input ).toHaveBeenCalled();
} );

it( 'should handle browser open failure', async () => {
Expand All @@ -132,7 +125,7 @@ describe( 'Auth Login Command', () => {
const { runCommand } = await import( '../login' );
await runCommand();

expect( password ).toHaveBeenCalled();
expect( input ).toHaveBeenCalled();
} );

it( 'should handle API error when fetching user info', async () => {
Expand Down
1 change: 1 addition & 0 deletions common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const DAY_MS = HOUR_MS * 24;
// OAuth constants
export const CLIENT_ID = '95109';
export const PROTOCOL_PREFIX = 'wpcom-local-dev';
export const DEFAULT_TOKEN_LIFETIME_MS = DAY_MS * 14;

export const LOCKFILE_NAME = 'appdata-v1.json.lock';
export const LOCKFILE_STALE_TIME = 5000;
Expand Down
1 change: 0 additions & 1 deletion common/logger-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

export enum AuthCommandLoggerAction {
LOGIN = 'login',
LOGOUT = 'logout',
}

export enum PreviewCommandLoggerAction {
Expand Down