Skip to content

Prompt inline account selection when auth fails for wrong account#7329

Open
VitaliiMotorko wants to merge 1 commit intoShopify:mainfrom
VitaliiMotorko:inline-account-switch-on-auth-error
Open

Prompt inline account selection when auth fails for wrong account#7329
VitaliiMotorko wants to merge 1 commit intoShopify:mainfrom
VitaliiMotorko:inline-account-switch-on-auth-error

Conversation

@VitaliiMotorko
Copy link
Copy Markdown

WHY

When running commands like shopify theme dev -s store while logged into the wrong account, the CLI shows an error message suggesting to run shopify auth login separately. This requires the user to:

  1. Read the error
  2. Run shopify auth login
  3. Select the correct account
  4. Re-run the original command

This is unnecessary friction for developers working with multiple stores/accounts.

WHAT

When authentication fails with an InvalidTargetError (wrong account for the target store), the CLI now:

  1. Shows a warning with the error message
  2. Immediately presents the account selection prompt (same UI as shopify auth login)
  3. Retries authentication with the newly selected account
  4. Continues the original command — no need to re-run

This only triggers in interactive terminals. CI and non-interactive environments preserve the existing error-and-exit behavior.

Changes

  • exchange.ts: Export InvalidTargetError so it can be caught upstream
  • session.ts: Wrap ensureAuthenticated with retry logic that catches InvalidTargetError, shows account selector via promptSessionSelect(), and retries once
  • session.test.ts: 5 new tests covering retry on full auth, retry on refresh, noPrompt, non-interactive terminal, and double-failure scenarios

Before

╭─ error ─────────────────────────────────────────────╮
│                                                      │
│  You are not authorized to use the CLI to develop    │
│  in the provided store: my-store                     │
│                                                      │
│  Next steps                                          │
│    • Run `shopify auth login`                        │
│                                                      │
╰──────────────────────────────────────────────────────╯

After

╭─ warning ────────────────────────────────────────────╮
│                                                      │
│  You are not authorized to use the CLI to develop    │
│  in the provided store: my-store                     │
│                                                      │
╰──────────────────────────────────────────────────────╯

? Which account would you like to use?
> work@example.com
  personal@example.com
  Log in with a different account

How to test

  1. Log in with shopify auth login
  2. Run shopify theme dev -s <store-you-dont-have-access-to>
  3. Verify the account selection prompt appears inline after the warning
  4. Select a different account and verify the command continues

Checklist

  • Considered cross-platform impacts (Mac, Linux, Windows) — uses existing terminalSupportsPrompting() guard
  • Added changeset via pnpm changeset add
  • CI/non-interactive environments are unaffected (tested with noPrompt: true)

When a command like `shopify theme dev` fails with an authorization error
because the user is logged into the wrong account, instead of just showing
an error and requiring a separate `shopify auth login`, the CLI now shows
the account selection prompt inline and retries authentication.

This only applies to interactive terminals — CI and non-interactive
environments preserve the existing error behavior.
@VitaliiMotorko VitaliiMotorko requested review from a team as code owners April 16, 2026 13:46
Copilot AI review requested due to automatic review settings April 16, 2026 13:46
@VitaliiMotorko
Copy link
Copy Markdown
Author

I have signed the CLA!

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR reduces friction when a user is authenticated to the “wrong” account for a target store by, on InvalidTargetError in interactive terminals, showing a warning, prompting for account selection inline, and retrying authentication once.

Changes:

  • Export InvalidTargetError from the token exchange module so it can be handled upstream.
  • Wrap ensureAuthenticated with retry logic that prompts for account selection (interactive only) on InvalidTargetError.
  • Add unit tests covering retry/no-prompt/non-interactive/double-failure scenarios.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
packages/cli-kit/src/private/node/session/exchange.ts Exports InvalidTargetError for upstream handling.
packages/cli-kit/src/private/node/session.ts Adds InvalidTargetError catch → warning + account prompt + single retry (interactive only).
packages/cli-kit/src/private/node/session.test.ts Adds tests validating retry and guard conditions.
.changeset/prompt-account-switch-on-auth-error.md Adds changeset documenting the new behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 201 to +205
export async function ensureAuthenticated(
applications: OAuthApplications,
_env?: NodeJS.ProcessEnv,
options: EnsureAuthenticatedAdditionalOptions = {},
): Promise<OAuthSession> {
Copy link

Copilot AI Apr 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ensureAuthenticated still accepts an _env parameter (and the JSDoc says it affects behavior), but it’s not used anywhere in the implementation. This makes the API misleading (callers like public/node/session.ts pass an env expecting it to be honored). Consider either wiring env through to the helpers that read from process.env (e.g., firstPartyDev, getIdentityTokenInformation, etc.) or removing the parameter/JSDoc to avoid implying it has an effect.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, but this is pre-existing behavior — the _env parameter was already unused (prefixed with _) before this PR. This change preserves the existing function signature to avoid breaking callers. Removing or wiring up _env would be a separate refactor outside the scope of this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants