-
Notifications
You must be signed in to change notification settings - Fork 286
Add store auth list for local store auth sessions #7714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
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
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@shopify/cli': minor | ||
| '@shopify/store': minor | ||
| --- | ||
|
|
||
| Add `shopify auth list` to list stores authenticated directly with `shopify store auth`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import StoreAuthList from './list.js' | ||
| import {listStoreAuthSessions} from '../../../services/store/auth/list.js' | ||
| import {writeStoreAuthListResult} from '../../../services/store/auth/list-result.js' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
|
|
||
| vi.mock('../../../services/store/auth/list.js') | ||
| vi.mock('../../../services/store/auth/list-result.js') | ||
|
|
||
| describe('store auth list command', () => { | ||
| test('lists direct store-auth sessions and writes text output by default', async () => { | ||
| vi.mocked(listStoreAuthSessions).mockReturnValue({sessions: []}) | ||
|
|
||
| await StoreAuthList.run([]) | ||
|
|
||
| expect(listStoreAuthSessions).toHaveBeenCalledWith() | ||
| expect(writeStoreAuthListResult).toHaveBeenCalledWith({sessions: []}, 'text') | ||
| }) | ||
|
|
||
| test('writes json output when requested', async () => { | ||
| vi.mocked(listStoreAuthSessions).mockReturnValue({sessions: []}) | ||
|
|
||
| await StoreAuthList.run(['--json']) | ||
|
|
||
| expect(writeStoreAuthListResult).toHaveBeenCalledWith({sessions: []}, 'json') | ||
| }) | ||
|
|
||
| test('does not expose organization or source-selection flags', () => { | ||
| expect(StoreAuthList.hidden).toBe(true) | ||
| expect(StoreAuthList.flags.json).toBeDefined() | ||
| expect(StoreAuthList.flags).not.toHaveProperty('organization-id') | ||
| expect(StoreAuthList.flags).not.toHaveProperty('from') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import {listStoreAuthSessions} from '../../../services/store/auth/list.js' | ||
| import {writeStoreAuthListResult} from '../../../services/store/auth/list-result.js' | ||
| import Command from '@shopify/cli-kit/node/base-command' | ||
| import {globalFlags, jsonFlag} from '@shopify/cli-kit/node/cli' | ||
|
|
||
| export default class StoreAuthList extends Command { | ||
| static hidden = true | ||
|
|
||
| static summary = 'List stores authenticated directly with store auth.' | ||
|
|
||
| static descriptionWithMarkdown = `Lists stores authenticated directly on this machine with \`shopify store auth\`. | ||
|
|
||
| Use this command to find stores that can be used with store-authenticated commands such as \`shopify store execute\`. | ||
| To list stores in a Shopify organization, run \`shopify store list\`.` | ||
|
|
||
| static description = this.descriptionWithoutMarkdown() | ||
|
|
||
| static examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> --json'] | ||
|
|
||
| static flags = { | ||
| ...globalFlags, | ||
| ...jsonFlag, | ||
| } | ||
|
|
||
| async run(): Promise<void> { | ||
| const {flags} = await this.parse(StoreAuthList) | ||
| const result = listStoreAuthSessions() | ||
|
|
||
| writeStoreAuthListResult(result, flags.json ? 'json' : 'text') | ||
| } | ||
| } |
93 changes: 93 additions & 0 deletions
93
packages/store/src/cli/services/store/auth/list-result.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import {writeStoreAuthListResult} from './list-result.js' | ||
| import {beforeEach, describe, expect, test} from 'vitest' | ||
| import {mockAndCaptureOutput} from '@shopify/cli-kit/node/testing/output' | ||
|
|
||
| describe('writeStoreAuthListResult', () => { | ||
| beforeEach(() => { | ||
| mockAndCaptureOutput().clear() | ||
| }) | ||
|
|
||
| test('renders direct store-auth sessions with subdomain and connected date', () => { | ||
| const output = mockAndCaptureOutput() | ||
|
|
||
| writeStoreAuthListResult( | ||
| { | ||
| sessions: [ | ||
| { | ||
| kind: 'store', | ||
| store: 'my-shop.myshopify.com', | ||
| userId: '42', | ||
| scopes: ['read_products', 'write_products'], | ||
| connectedAt: '2026-05-22T00:00:00Z', | ||
| associatedUser: {id: 42, email: 'merchant@example.com'}, | ||
| }, | ||
| ], | ||
| }, | ||
| 'text', | ||
| ) | ||
|
|
||
| expect(output.info()).toContain('Subdomain') | ||
| expect(output.info()).toContain('Connected') | ||
| expect(output.info()).toContain('my-shop') | ||
| expect(output.info()).not.toContain('my-shop.myshopify.com') | ||
| expect(output.info()).toContain('May 22, 2026') | ||
| expect(output.info()).not.toContain('merchant@example.com') | ||
| expect(output.info()).not.toContain('read_products, write_products') | ||
| expect(output.info()).not.toContain('shopify store list') | ||
| }) | ||
|
|
||
| test('renders an empty state with auth and organization-list guidance', () => { | ||
| const output = mockAndCaptureOutput() | ||
|
|
||
| writeStoreAuthListResult({sessions: []}, 'text') | ||
|
|
||
| expect(output.info()).toContain('No stores are authenticated directly with `shopify store auth`.') | ||
| expect(output.info()).toContain('shopify store auth --store <domain> --scopes <scopes>') | ||
| expect(output.info()).toContain('shopify store list') | ||
| }) | ||
|
|
||
| test('writes a deterministic JSON document with only subdomain and connected date', () => { | ||
| const output = mockAndCaptureOutput() | ||
|
|
||
| writeStoreAuthListResult( | ||
| { | ||
| sessions: [ | ||
| { | ||
| kind: 'store', | ||
| store: 'shop.myshopify.com', | ||
| userId: '42', | ||
| scopes: ['read_products'], | ||
| connectedAt: '2026-05-22T00:00:00Z', | ||
| associatedUser: {id: 42, email: 'merchant@example.com'}, | ||
| }, | ||
| ], | ||
| }, | ||
| 'json', | ||
| ) | ||
|
|
||
| expect(JSON.parse(output.output())).toEqual({ | ||
| sessions: [ | ||
| { | ||
| subdomain: 'shop', | ||
| connected: 'May 22, 2026', | ||
| }, | ||
| ], | ||
| }) | ||
| }) | ||
|
|
||
| test('includes empty-state guidance in JSON output when there are no sessions', () => { | ||
| const output = mockAndCaptureOutput() | ||
|
|
||
| writeStoreAuthListResult({sessions: []}, 'json') | ||
|
|
||
| expect(JSON.parse(output.output())).toEqual({ | ||
| sessions: [], | ||
| message: [ | ||
| 'No stores are authenticated directly with `shopify store auth`.', | ||
| '', | ||
| 'Run `shopify store auth --store <domain> --scopes <scopes>` to authenticate a store.', | ||
| 'Run `shopify store list` to list stores in a Shopify organization.', | ||
| ].join('\n'), | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import {type StoreAuthListResult} from './list.js' | ||
| import {extractSubdomain, formatShortDate} from '../display.js' | ||
| import {outputInfo, outputResult} from '@shopify/cli-kit/node/output' | ||
| import {renderTable} from '@shopify/cli-kit/node/ui' | ||
|
|
||
| export function writeStoreAuthListResult(result: StoreAuthListResult, format: 'text' | 'json'): void { | ||
| if (format === 'json') { | ||
| outputResult(JSON.stringify(toJsonResult(result), null, 2)) | ||
| return | ||
| } | ||
|
|
||
| renderTextResult(result) | ||
| } | ||
|
|
||
| function toJsonResult(result: StoreAuthListResult): { | ||
| sessions: ReturnType<typeof toDisplaySession>[] | ||
| message?: string | ||
| } { | ||
| return { | ||
| sessions: result.sessions.map(toDisplaySession), | ||
| ...(result.sessions.length === 0 ? {message: emptyStateMessage()} : {}), | ||
| } | ||
| } | ||
|
|
||
| function renderTextResult(result: StoreAuthListResult): void { | ||
| if (result.sessions.length === 0) { | ||
| outputInfo(emptyStateMessage()) | ||
| return | ||
| } | ||
|
|
||
| renderTable({ | ||
| rows: result.sessions.map(toDisplaySession), | ||
| columns: { | ||
| subdomain: {header: 'Subdomain'}, | ||
| connected: {header: 'Connected'}, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| function toDisplaySession(session: StoreAuthListResult['sessions'][number]): {subdomain: string; connected: string} { | ||
| return { | ||
| subdomain: extractSubdomain(session.store) ?? session.store, | ||
| connected: formatShortDate(session.connectedAt), | ||
| } | ||
| } | ||
|
|
||
| function emptyStateMessage(): string { | ||
| return [ | ||
| 'No stores are authenticated directly with `shopify store auth`.', | ||
| '', | ||
| 'Run `shopify store auth --store <domain> --scopes <scopes>` to authenticate a store.', | ||
| 'Run `shopify store list` to list stores in a Shopify organization.', | ||
| ].join('\n') | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import {listStoreAuthSessions} from './list.js' | ||
| import {listStoredStoreAuthSummaries} from './stored-auth.js' | ||
| import {describe, expect, test, vi} from 'vitest' | ||
|
|
||
| vi.mock('./stored-auth.js') | ||
|
|
||
| describe('listStoreAuthSessions', () => { | ||
| test('projects stored store auth summaries into typed auth sessions', () => { | ||
| vi.mocked(listStoredStoreAuthSummaries).mockReturnValue([ | ||
| { | ||
| store: 'shop.myshopify.com', | ||
| userId: '42', | ||
| scopes: ['read_products'], | ||
| acquiredAt: '2026-03-27T00:00:00.000Z', | ||
| expiresAt: '2026-03-28T00:00:00.000Z', | ||
| refreshTokenExpiresAt: '2026-04-28T00:00:00.000Z', | ||
| associatedUser: {id: 42, email: 'merchant@example.com'}, | ||
| }, | ||
| ]) | ||
|
|
||
| expect(listStoreAuthSessions()).toEqual({ | ||
| sessions: [ | ||
| { | ||
| kind: 'store', | ||
| store: 'shop.myshopify.com', | ||
| userId: '42', | ||
| scopes: ['read_products'], | ||
| connectedAt: '2026-03-27T00:00:00.000Z', | ||
| expiresAt: '2026-03-28T00:00:00.000Z', | ||
| refreshTokenExpiresAt: '2026-04-28T00:00:00.000Z', | ||
| associatedUser: {id: 42, email: 'merchant@example.com'}, | ||
| }, | ||
| ], | ||
| }) | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| import {listStoredStoreAuthSummaries, type StoredStoreAuthSummary} from './stored-auth.js' | ||
|
|
||
| export interface StoreAuthListEntry { | ||
| kind: 'store' | ||
| store: string | ||
| userId: string | ||
| scopes: string[] | ||
| connectedAt: string | ||
| expiresAt?: string | ||
| refreshTokenExpiresAt?: string | ||
| associatedUser?: StoredStoreAuthSummary['associatedUser'] | ||
| } | ||
|
|
||
| export interface StoreAuthListResult { | ||
| sessions: StoreAuthListEntry[] | ||
| } | ||
|
|
||
| export function listStoreAuthSessions(): StoreAuthListResult { | ||
| return { | ||
| sessions: listStoredStoreAuthSummaries().map((summary) => ({ | ||
| kind: 'store', | ||
| store: summary.store, | ||
| userId: summary.userId, | ||
| scopes: summary.scopes, | ||
| connectedAt: summary.acquiredAt, | ||
| ...(summary.expiresAt ? {expiresAt: summary.expiresAt} : {}), | ||
| ...(summary.refreshTokenExpiresAt ? {refreshTokenExpiresAt: summary.refreshTokenExpiresAt} : {}), | ||
| ...(summary.associatedUser ? {associatedUser: summary.associatedUser} : {}), | ||
| })), | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.