Skip to content

feat(auth): add tw auth token view via cli-core attachTokenViewCommand#227

Merged
scottlovegrove merged 2 commits into
mainfrom
feat/auth-token-view-cli-core
May 16, 2026
Merged

feat(auth): add tw auth token view via cli-core attachTokenViewCommand#227
scottlovegrove merged 2 commits into
mainfrom
feat/auth-token-view-cli-core

Conversation

@scottlovegrove
Copy link
Copy Markdown
Collaborator

Summary

  • Wire @doist/cli-core/auth's attachTokenViewCommand as a subcommand of the existing tw auth token [token] setter — tw auth token view prints the stored token to stdout (no envelope) so it is pipe-safe for shell composition (eval $(tw auth token view), etc.).
  • Mirrors the pattern todoist-cli adopted in Doist/todoist-cli#341; the next standalone step in the cli-core migration after refactor(auth): adopt cli-core 0.16 createSecureStore #226.
  • Commander matches subcommand names before the parent action, so tw auth token view always dispatches to the view path — Twist OAuth tokens are opaque random strings, so a literal "view" cannot collide with a real token value.
  • Refuses to print when TWIST_API_TOKEN is set (cli-core's TOKEN_FROM_ENV guard): the env var takes precedence elsewhere, so printing the saved token would disclose a secret the CLI did not manage.
  • --user <ref> flag is added by cli-core unconditionally; matches the lone stored account via the existing TwistTokenStore.active(ref) path (id-or-label match through parseRef), so single-user semantics are preserved.

No changes to the lib/ layer.

Test plan

  • npm run type-check
  • npm run lint:check
  • npm test — 5 new tests (happy path, TOKEN_FROM_ENV refusal, NOT_AUTHENTICATED, --user id match, --user id miss); 611 total pass
  • npm run build
  • tw auth token --help shows the new view subcommand
  • tw auth token view --help shows the --user <ref> flag
  • Manual end-to-end: tw auth logintw auth token view prints the bearer token → eval $(tw auth token view) pipes safely
  • Manual env-guard: TWIST_API_TOKEN=anything tw auth token view exits with TOKEN_FROM_ENV

🤖 Generated with Claude Code

Wire @doist/cli-core/auth's `attachTokenViewCommand` as a subcommand of
the existing `tw auth token [token]` setter — `tw auth token view`
prints the stored token to stdout (no envelope) so it is pipe-safe for
shell composition (`eval $(tw auth token view)`, etc.).

- Commander matches subcommand names before the parent action, so
  `tw auth token view` always dispatches to the view path; Twist OAuth
  tokens are opaque random strings, so a literal "view" cannot collide
  with a real token value.
- Refuses to print when TWIST_API_TOKEN is set (cli-core's
  `TOKEN_FROM_ENV` guard) — the env var takes precedence elsewhere so
  printing the saved token would disclose a secret the CLI did not
  manage.
- `--user <ref>` flag is exposed by cli-core unconditionally; matches
  the lone stored account via the existing `TwistTokenStore.active(ref)`
  path (id-or-label match through parseRef), so single-user semantics
  are preserved.

No changes to the lib/ layer. Five new tests cover the happy path,
TOKEN_FROM_ENV refusal, NOT_AUTHENTICATED, and --user id match/miss.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@doistbot doistbot requested a review from rfgamaral May 16, 2026 20:25
Copy link
Copy Markdown
Member

@doistbot doistbot left a comment

Choose a reason for hiding this comment

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

This PR successfully introduces the tw auth token view subcommand to enable pipe-safe token printing via @doist/cli-core. It is a solid addition that mirrors established patterns while properly respecting environment variable precedence. A few adjustments are needed, specifically syncing the new command in the AI skill reference, using Vitest's vi.stubEnv() to prevent environment state leakage in tests, and tightening the stdout assertions to fully guarantee pipe-safe output.

Share FeedbackReview Logs

Comment thread src/commands/auth/index.ts
Comment thread src/commands/auth/auth.test.ts Outdated
Comment thread src/commands/auth/auth.test.ts Outdated
- src/lib/skills/content.ts + regenerated skills/twist-cli/SKILL.md:
  update the agent-facing reference from the (always-wrong) `tw auth
  token --user <ref>` to the actual `tw auth token view [--user <ref>]`
  surface introduced by this PR.
- src/commands/auth/auth.test.ts: switch the TOKEN_FROM_ENV setup to
  `vi.stubEnv` / `vi.unstubAllEnvs` so a developer's exported
  TWIST_API_TOKEN is restored after the suite runs (no cross-test
  leakage). Tighten stdout assertions: replace `toHaveBeenCalledWith`
  on `process.stdout.write` with full-payload comparison via a
  `stdoutPayload()` helper, and assert `consoleSpy` was not called, so
  any future preamble/trailer added to the view output would fail the
  pipe-safety contract.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@scottlovegrove scottlovegrove self-assigned this May 16, 2026
@scottlovegrove scottlovegrove added the 👀 Show PR PR must be reviewed before or after merging label May 16, 2026
@scottlovegrove scottlovegrove merged commit 9ae4388 into main May 16, 2026
5 checks passed
@scottlovegrove scottlovegrove deleted the feat/auth-token-view-cli-core branch May 16, 2026 20:35
doist-release-bot Bot added a commit that referenced this pull request May 16, 2026
## [2.38.0](v2.37.0...v2.38.0) (2026-05-16)

### Features

* **auth:** add `tw auth token view` via cli-core attachTokenViewCommand ([#227](#227)) ([9ae4388](9ae4388))
@doist-release-bot
Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 2.38.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

released 👀 Show PR PR must be reviewed before or after merging

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants