Skip to content

feat(cli): surface env-var credentials in logout and whoami#1363

Merged
stefanjudis merged 5 commits into
mainfrom
feat/surface-env-credentials-in-auth-commands
Jun 26, 2026
Merged

feat(cli): surface env-var credentials in logout and whoami#1363
stefanjudis merged 5 commits into
mainfrom
feat/surface-env-credentials-in-auth-commands

Conversation

@stefanjudis

Copy link
Copy Markdown
Collaborator

Affected Components

  • CLI

Notes for the Reviewer

Problem

Credentials are resolved from two sources, with env vars winning over the persisted login session (config.ts):

getApiKey ()    { return process.env.CHECKLY_API_KEY    || this.auth.get('apiKey') || '' }
getAccountId () { return process.env.CHECKLY_ACCOUNT_ID || this.data.get('accountId') || '' }

logout only clears the persisted session (config.clear()) — it can't touch your shell or .env. So when CHECKLY_API_KEY/CHECKLY_ACCOUNT_ID are set, logout cheerfully says goodbye while you stay fully authenticated, and whoami keeps reporting a logged-in account afterward with no explanation. Because .env is loaded from the current working directory, the same whoami can even resolve a different account depending on which directory you run it in.

Why this shape

login already handles this — it warns and exits when those env vars are set, because they override any login session:

Warning: `CHECKLY_API_KEY` or `CHECKLY_ACCOUNT_ID` environment variables are configured
(via shell or .env file). You must delete them to use `npx checkly login`.

So the precedent and the wording already exist in one command. This PR simply extends that same transparency to the other two auth commands rather than inventing anything new. The shared sentence is extracted into common-messages.ts so all three stay in sync; login's output is unchanged.

Behavior (with env-var credentials set)

whoami — appends a note on stdout:

You are currently on account "Monitoring as Code" (b2f06fc6-…) as stefan@checklyhq.com.
Plan: Enterprise
Add-ons: Resolve Hobby, Communicate Hobby

This account is resolved from your environment, not a `checkly login` session. `CHECKLY_API_KEY` or `CHECKLY_ACCOUNT_ID` environment variables are configured (via shell or .env file).

logout — still clears the local session, then warns on stderr that you're not actually logged out:

See you soon! 👋
 ›   Warning: `CHECKLY_API_KEY` or `CHECKLY_ACCOUNT_ID` environment variables
 ›   are configured (via shell or .env file). You are still authenticated
 ›   through them until you remove them.

login — unchanged, now sourced from the shared message:

 ›   Warning: `CHECKLY_API_KEY` or `CHECKLY_ACCOUNT_ID` environment variables
 ›   are configured (via shell or .env file). You must delete them to use `npx
 ›   checkly login`.

Testing

  • lint and tsc --noEmit pass.
  • Updated the logout e2e (it asserted stderr === '', the old silent behavior) and the whoami e2e to assert the new note. The e2e env already authenticates via CHECKLY_API_KEY/CHECKLY_ACCOUNT_ID, so both paths exercise the env-var branch.
  • Manually verified all three commands against a real project with both env vars set; the outputs above are the actual results.

login already warns when CHECKLY_API_KEY/CHECKLY_ACCOUNT_ID are set,
since they override any persisted session. logout and whoami stayed
silent, so clearing the local session or running whoami after logout
still looked authenticated with no explanation.

Extract login's message into common-messages and reuse it so logout
warns that env vars keep you authenticated, and whoami notes when the
account is resolved from the environment.
oclif's this.warn() word-wraps the message and prefixes continuation
lines with ` › `, splitting "still authenticated through them" across
a line break. Strip the markers and collapse whitespace before matching
so the assertion is independent of terminal width.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 improves auth-command transparency in the CLI by surfacing when credentials are coming from CHECKLY_API_KEY / CHECKLY_ACCOUNT_ID (including via .env), which can override the persisted login session and make logout/whoami behavior confusing.

Changes:

  • Extracted the shared “env credentials configured” sentence into common-messages.ts and reused it in login, logout, and whoami.
  • Updated whoami to print an explicit stdout note when the active account is resolved from environment credentials.
  • Updated logout to warn (stderr) that the user remains authenticated via environment credentials after clearing the local session; adjusted e2e assertions accordingly.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
packages/cli/src/messages/common-messages.ts Adds a shared message for “env credentials configured” to keep wording consistent across commands.
packages/cli/src/commands/whoami.ts Appends a note when env vars are driving authentication instead of a checkly login session.
packages/cli/src/commands/logout.ts Warns after clearing local session if env vars still keep the user authenticated.
packages/cli/src/commands/login.ts Reuses the shared env-credentials warning message without changing behavior.
packages/cli/e2e/tests/whoami.spec.ts Asserts the new env-credentials note appears in whoami output.
packages/cli/e2e/tests/logout.spec.ts Updates assertions to expect a warning on stderr when env vars are configured.

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

Match the substring style used by the login e2e instead of normalizing
oclif's line wrapping. "authenticated" appears only in the logout
suffix and is a single word, so it can't be split across a wrapped
line.
@stefanjudis stefanjudis merged commit 19db98f into main Jun 26, 2026
33 of 37 checks passed
@stefanjudis stefanjudis deleted the feat/surface-env-credentials-in-auth-commands branch June 26, 2026 12:06
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.

3 participants