feat(auth): auto-trigger login flow when authentication required#170
Merged
feat(auth): auto-trigger login flow when authentication required#170
Conversation
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
Contributor
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Dsn
Other
Bug Fixes 🐛
Documentation 📚
Internal Changes 🔧
Other
🤖 This preview updates automatically when you update the PR. |
Contributor
Codecov Results 📊❌ Patch coverage is 50.00%. Project has 1748 uncovered lines. Files with missing lines (34)
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.
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
BYK
reviewed
Feb 5, 2026
- 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
There was a problem hiding this comment.
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.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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
src/lib/interactive-login.tsfor reuseauth logincommand to use the shared helperexecuteWithAutoAuth()wrapper inbin.tsthat catchesAuthError("not_authenticated")and triggers loginTest Plan
sentry auth logoutsentry issue listNon-TTY test: