Skip to content

feat(auth): auto-trigger login flow when authentication required#170

Merged
betegon merged 12 commits intomainfrom
feat/auto-auth-on-unauthenticated
Feb 5, 2026
Merged

feat(auth): auto-trigger login flow when authentication required#170
betegon merged 12 commits intomainfrom
feat/auto-auth-on-unauthenticated

Conversation

@betegon
Copy link
Member

@betegon betegon commented Feb 4, 2026

Summary

When a command requires authentication but the user isn't logged in, automatically start the OAuth device flow instead of showing an error. After successful login, the original command retries automatically.

Only triggers in interactive TTY environments - non-TTY sessions (CI, scripts, piped input) continue showing the error message as before.

Changes

  • Extract device flow UI logic into src/lib/interactive-login.ts for reuse
  • Refactor auth login command to use the shared helper
  • Add executeWithAutoAuth() wrapper in bin.ts that catches AuthError("not_authenticated") and triggers login

Test Plan

  1. Log out: sentry auth logout
  2. Run a command that requires auth: sentry issue list
  3. Should see "Authentication required. Starting login flow..." and the OAuth device flow starts
  4. Complete login in browser
  5. Original command should automatically retry and succeed

Non-TTY test:

echo "" | sentry issue list
# Should show: "Error: Not authenticated. Run 'sentry auth login' first."

When a command requires authentication but the user isn't logged in,
automatically start the OAuth device flow instead of just showing an
error message. After successful login, the original command is retried.

This only triggers in interactive TTY environments. Non-TTY sessions
(CI, scripts, piped) continue to show the error message.

Changes:
- Extract interactive login flow into reusable helper
- Refactor auth login command to use the shared helper
- Add auto-auth logic in bin.ts error handler
@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Semver Impact of This PR

🟡 Minor (new features)

📋 Changelog Preview

This is how your changes will appear in the changelog.
Entries from this PR are highlighted with a left border (blockquote style).


New Features ✨

Dsn

  • Infer project from directory name when DSN detection fails by BYK in #178
  • Add project root detection for automatic DSN discovery by BYK in #159

Other

  • (auth) Auto-trigger login flow when authentication required by betegon in #170
  • (commands) Add sentry log command by betegon in #160
  • (db) Add schema repair and sentry cli fix command by BYK in #197
  • (issue) Replace --org/--project flags with /ID syntax by BYK in #161
  • (lib) Add anyTrue helper for parallel-with-early-exit pattern by BYK in #174
  • (telemetry) Add withTracing helper to reduce Sentry span boilerplate by BYK in #172

Bug Fixes 🐛

  • (types) Align schema types with Sentry API by betegon in #169
  • Corrected the codecov action script by MathurAditya724 in #201
  • Improved the plan command by MathurAditya724 in #185
  • Use ASCII arrow for consistent terminal rendering by BYK in #192
  • Corrected the rendering and props for the span tree by MathurAditya724 in #184
  • ParseIssueArg now checks slashes before dashes by BYK in #177
  • Address bugbot review comments on dsn-cache model-based tests by BYK in #176
  • Added nullable in substatus's zod validation by MathurAditya724 in #157

Documentation 📚

  • Update AGENTS.md with testing guidelines and architecture by BYK in #190

Internal Changes 🔧

  • (upgrade) Use centralized user-agent for GitHub API requests by BYK in #173

Other

  • test: add comprehensive tests for resolve-target module by BYK in #199
  • test: add tests for executeUpgrade with unknown method by BYK in #198
  • test: expand version check test coverage by BYK in #196
  • test: add comprehensive tests for DSN errors and resolver by BYK in #195
  • test: add comprehensive tests for human formatter detail functions by BYK in #194
  • test: add comprehensive tests for human formatter utilities by BYK in #191
  • test: add coverage for fetchLatestVersion and versionExists by BYK in #189
  • test: add coverage for UpgradeError and SeerError classes by BYK in #188
  • test: add property tests for sentry-urls.ts (Phase 3) by BYK in #186
  • test: simplify issue-id tests covered by property tests by BYK in #183
  • test: simplify alias and arg-parsing tests covered by property tests by BYK in #182
  • test: add property tests for API command and human formatters by BYK in #181
  • test: remove redundant DB tests covered by model-based tests by BYK in #180
  • test: add property tests for async utilities (Phase 4) by BYK in #179
  • test: add model-based tests for DSN and project cache by BYK in #171
  • test: add model-based and property-based testing with fast-check by BYK in #166

🤖 This preview updates automatically when you update the PR.

@github-actions
Copy link
Contributor

github-actions bot commented Feb 4, 2026

Codecov Results 📊

❌ Patch coverage is 50.00%. Project has 1748 uncovered lines.
❌ Project coverage is 77.97%. Comparing base (base) to head (head).

Files with missing lines (34)
File Patch % Lines
human.ts 53.59% ⚠️ 388 Missing
resolve-target.ts 15.28% ⚠️ 366 Missing
oauth.ts 25.10% ⚠️ 194 Missing
api-client.ts 70.62% ⚠️ 176 Missing
upgrade.ts 45.49% ⚠️ 139 Missing
migration.ts 47.44% ⚠️ 82 Missing
telemetry.ts 77.51% ⚠️ 56 Missing
api.ts 89.80% ⚠️ 47 Missing
seer.ts 75.54% ⚠️ 45 Missing
schema.ts 89.56% ⚠️ 40 Missing
seer.ts 79.87% ⚠️ 30 Missing
preload.ts 53.23% ⚠️ 29 Missing
utils.ts 88.94% ⚠️ 25 Missing
detector.ts 90.10% ⚠️ 20 Missing
code-scanner.ts 95.00% ⚠️ 16 Missing
arg-parsing.ts 90.00% ⚠️ 12 Missing
dsn-cache.ts 94.62% ⚠️ 12 Missing
fix.ts 83.61% ⚠️ 10 Missing
fs-utils.ts 57.14% ⚠️ 9 Missing
project-root.ts 97.73% ⚠️ 7 Missing
version-check.ts 91.14% ⚠️ 7 Missing
feedback.ts 84.21% ⚠️ 6 Missing
auth.ts 95.52% ⚠️ 6 Missing
upgrade.ts 93.83% ⚠️ 5 Missing
resolver.ts 94.57% ⚠️ 5 Missing
index.ts 95.96% ⚠️ 4 Missing
env-file.ts 97.58% ⚠️ 3 Missing
project-aliases.ts 97.40% ⚠️ 2 Missing
project-root-cache.ts 96.92% ⚠️ 2 Missing
alias.ts 99.28% ⚠️ 1 Missing
parser.ts 98.63% ⚠️ 1 Missing
colors.ts 97.96% ⚠️ 1 Missing
helpers.ts 94.74% ⚠️ 1 Missing
helpers.ts 94.74% ⚠️ 1 Missing
Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
- Coverage    77.99%    77.97%    -0.02%
==========================================
  Files           58        58         —
  Lines         7922      7933       +11
  Branches         0         0         —
==========================================
+ Hits          6178      6185        +7
- Misses        1744      1748        +4
- Partials         0         0         —

Generated by Codecov Action

process.stdin.isTTY returns undefined in Bun when running TypeScript
files directly, causing the auto-auth condition to always fail.

Using isatty(0) from node:tty provides reliable TTY detection across
both Node.js and Bun runtimes.

Also fixes stdout/stderr consistency for status messages.
Stricli catches all command errors internally and formats them via
exceptionWhileRunningCommand, preventing error propagation to our
catch block in bin.ts.

This change re-throws AuthError with reason 'not_authenticated' from
the error formatter, allowing it to propagate to executeWithAutoAuth
where we can trigger the interactive login flow.
@betegon betegon marked this pull request as ready for review February 5, 2026 13:14
Prevents confusing loop when 'sentry auth login' fails - the error would
trigger another login attempt. Auth commands (login, status, refresh)
now handle their own auth flow without auto-login interference.
…blocks to run

Replaces direct process.exit() calls with process.exitCode + return pattern.
This ensures the finally block in main() runs, properly cleaning up the
background version check HTTP request.
- Add stderr parameter to runInteractiveLogin for proper stream separation
- Use formatError() instead of inline CliError/Error checks
- Follows codebase pattern: stdout for UI, stderr for errors
When runInteractiveLogin fails, it already displays the error to stderr.
Don't throw another AuthError - just set exit code and return.
This is an expected state (user not logged in), not an error.
- Move re-throw check before Sentry.captureException in app.ts
- Filter out from telemetry.ts catch block
- Don't mark session as 'crashed' for expected auth state
@betegon betegon requested a review from BYK February 5, 2026 15:31
- Add skipAutoAuth option to AuthError for commands that don't want auto-login
- auth status uses skipAutoAuth since user explicitly checks auth state
- bin.ts checks err.skipAutoAuth instead of args[0] === 'auth'
- Extensible: new commands get auto-login by default, opt-out if needed
@betegon betegon merged commit 6d48fda into main Feb 5, 2026
24 of 25 checks passed
@betegon betegon deleted the feat/auto-auth-on-unauthenticated branch February 5, 2026 19:45
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

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

Comments