Skip to content

fix(auth): whoami never reports an unconfirmed or dead session as signed in - #277

Merged
AmanVarshney01 merged 3 commits into
mainfrom
fix/auth-whoami-ended-session
Sep 21, 2026
Merged

AmanVarshney01 merged 3 commits into
mainfrom
fix/auth-whoami-ended-session

Conversation

@AmanVarshney01

@AmanVarshney01 AmanVarshney01 commented Sep 21, 2026

Copy link
Copy Markdown
Member

auth whoami enriches its answer with GET /v1/me and swallowed every failure so it works offline. That gave two false answers:

  1. It swallowed the engine's definitive verdict that the session is dead (refresh refused with invalid_grant, session already cleared) and printed authenticated: true. create-prisma trusts that, skips sign-in, and its next command fails with CLI.CREDENTIALS_REQUIRED.
  2. When the lookup was inconclusive (3s timeout, network error, 5xx) it printed authenticated: true with no hint that nothing had been confirmed.

For comparison, wrangler whoami, netlify status, vercel whoami and gh auth status never report "logged in" without an API confirmation in that run; gh reports a distinct "timeout" state. Prisma's whoami keeps its offline behaviour but now says when the answer is unconfirmed.

Changes, discriminated by the structured error's code (CliStructuredError.is), never by message:

  • CLI.CREDENTIALS_REQUIRED from the lookup → signed out: authenticated: false, exit 0, the existing Sign in next action.
  • AUTH.SERVICE_TOKEN_REJECTED → settles as itself (exit 2); signing in cannot fix an environment token.
  • New result field verified: true only when the API accepted the credential during this run. An inconclusive lookup still answers from the credential's claims, with verified: false and one notice line in human output: "Could not reach Prisma to confirm this sign-in. Showing what the local credential says."
{"authenticated":true,"verified":false,"workspace":{},"user":{},"source":"stored","expiresAt":null}

To review: (a) verified is an additive result field; (b) the test that pinned "a 401 on an env token falls back to claims" now asserts AUTH.SERVICE_TOKEN_REJECTED, exit 2.

No new engine API or flag; exit codes and the 3s bound are unchanged. Checked with the built binary against a local server for all three cases. Lint, typecheck, cli and engine tests, check:error-reference, check:grammar, check:conformance pass; e2e 6 passed, 48 skipped (no E2E token locally). Not verified against the real auth service.

🤖 Generated with Claude Code

…signed in

`auth whoami` looks the identity up with GET /v1/me as a best-effort
enrichment and swallowed every failure of it, so it works offline. That
blanket catch also swallowed the engine's definitive verdict on the
credential: a refresh refused with invalid_grant (the stored session
already compare-and-cleared), a credential that could never be renewed,
or a session ended underneath the process. whoami then printed
`authenticated: true` with the dead session's workspace. Automation
trusts that answer: create-prisma runs `prisma auth whoami --json`,
skips sign-in, and its next command fails with CLI.CREDENTIALS_REQUIRED.

The lookup now tells the verdict apart from an outage by the structured
error's code, as the engine raised it, never by origin or message:

- CLI.CREDENTIALS_REQUIRED reads as signed out: `authenticated: false`,
  exit 0, the existing Sign in next action, the same result an
  unauthenticated user gets. This is the question whoami exists to
  answer, and it is what legacy did for a 401 on its lookup (1271c57).
- AUTH.SERVICE_TOKEN_REJECTED settles as itself, exit 2, as
  AUTH.SERVICE_TOKEN_EMPTY already does for this command. Signing in
  cannot help while PRISMA_SERVICE_TOKEN is set, so "signed out" with a
  Sign in action would send the reader the wrong way.
- Everything else (network error, timeout, 5xx, CLI.AUTH_SERVICE_ERROR)
  still answers from the credential's own claims, unchanged. Ctrl-C
  still outranks whatever the interrupted request threw.

No new engine API, result field, or flag. The one existing test that
pinned the old behaviour for a 401 on an environment token now asserts
the rejection; the never-answering-host test beside it keeps covering
the claims fallback for that credential.

Verified with the built binary against a local HTTP server: 401 plus
invalid_grant gives `authenticated: false` and an emptied state file;
401 plus a 503 token endpoint still gives the claims and leaves the
state file untouched.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Sep 21, 2026

Copy link
Copy Markdown

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Essentials

Run ID: 2f9be3fb-1640-4f83-8453-451a3a46ffac

📥 Commits

Reviewing files that changed from the base of the PR and between 21870fd and ee555ed.

📒 Files selected for processing (5)
  • docs/reference/error-reference.md
  • packages/cli/src/commands/auth/credential-card.ts
  • packages/cli/src/commands/auth/whoami.ts
  • packages/cli/tests/auth.test.ts
  • packages/cli/tests/whoami.test.ts

Included review availability: 1 review is currently available. Your included PR review attempts over the past 7 days set your current allowance at 5 reviews per hour.


Summary by CodeRabbit

  • New Features

    • auth whoami now clearly distinguishes verified, signed-out, and unverified credential states.
    • JSON output indicates whether the current identity was confirmed online.
    • Temporary authentication-service issues preserve locally detected credentials while displaying an informational warning.
  • Bug Fixes

    • Rejected service tokens now return an authentication error instead of being accepted based only on local token details.
    • Expired or missing credentials are correctly reported as signed out.
  • Documentation

    • Updated authentication error reference guidance.

Walkthrough

auth whoami now distinguishes confirmed, signed-out, and inconclusive credential lookups. It reports verified based on online confirmation, preserves stored claims after transient failures, and rethrows rejected service-token errors. Human output shows a notice for unverified credentials. Tests and error-reference documentation cover the updated behavior.

Priority: ⬇️ Low

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 4 files. (1 skipped: 1… Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: preventing auth whoami from reporting dead or unconfirmed sessions as signed in.
Description check ✅ Passed The description directly explains the authentication-state changes, error handling, verified field, fallback behavior, and test coverage.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Full details: Docstring Coverage

Explanation

Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 4 files. (1 skipped: 1 unsupported.)

  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
✨ Simplify code
  • Commit to this branch
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@pkg-pr-new

pkg-pr-new Bot commented Sep 21, 2026

Copy link
Copy Markdown

Open in StackBlitz

npx https://pkg.pr.new/@prisma/cli@277
npx https://pkg.pr.new/@prisma/cli-engine@277

commit: ee555ed

AmanVarshney01 and others added 2 commits September 21, 2026 16:31
Shorten the comments in whoami.ts and keep fetchedIdentity and the
handler's `credential` name, so the source diff stays small. Drop the
error-conventions passage, restore the parity record, and keep one
sentence per touched error-reference entry. Keep three tests: expired
session reads as signed out, a refused env token settles as itself,
and a transient auth-service failure still answers from the claims.
Behaviour is unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
After an inconclusive /v1/me lookup (timeout, network error, 5xx,
CLI.AUTH_SERVICE_ERROR) whoami still answered `authenticated: true`
from the credential's claims with no hint that nothing was verified.

The result gains `verified`: true only when the API accepted the
credential during this run, false when answering from claims and
whenever `authenticated` is false. Human output adds one info line for
a held but unverified credential. Exit codes, `authenticated`, the 3s
bound and the plain stdout rows are unchanged.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@AmanVarshney01 AmanVarshney01 changed the title fix(auth): whoami stops reporting a session the engine found dead as signed in fix(auth): whoami never reports an unconfirmed or dead session as signed in Sep 21, 2026
@AmanVarshney01
AmanVarshney01 marked this pull request as ready for review September 21, 2026 13:53
@AmanVarshney01
AmanVarshney01 merged commit 1d3437f into main Sep 21, 2026
16 of 17 checks passed
@AmanVarshney01
AmanVarshney01 deleted the fix/auth-whoami-ended-session branch September 21, 2026 15:18
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