Skip to content

Commit 19db98f

Browse files
authored
feat(cli): surface env-var credentials in logout and whoami (#1363)
* feat(cli): surface env-var credentials in logout and whoami 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. * test(cli): normalize oclif warning wrap in logout e2e 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. * test(cli): assert logout warning with wrap-safe substring 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. * test(cli): drop redundant logout warning substring assertion * test(cli): trim verbose comments in auth e2e tests
1 parent 5579d16 commit 19db98f

6 files changed

Lines changed: 18 additions & 3 deletions

File tree

packages/cli/e2e/__tests__/logout.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe('logout', () => {
2121
})
2222

2323
expect(stdout).toContain('See you soon! 👋')
24-
expect(stderr).toBe('')
24+
// env credentials are set, so logout warns the session clear didn't log us out
25+
expect(stderr).toContain('are configured (via shell or .env file)')
2526
})
2627
})

packages/cli/e2e/__tests__/whoami.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ describe('whomai', () => {
1919
const { stdout } = await runCheckly(fixt, ['whoami'])
2020
expect(stdout).toContain(config.get('accountName'))
2121
expect(stdout).toContain('Plan:')
22+
// env credentials are set, so whoami notes the account comes from the environment
23+
expect(stdout).toContain('resolved from your environment')
2224
})
2325
})

packages/cli/src/commands/login.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import config from '../services/config.js'
66
import * as api from '../rest/api.js'
77
import type { Account } from '../rest/accounts.js'
88
import { AuthContext } from '../auth/index.js'
9+
import commonMessages from '../messages/common-messages.js'
910

1011
export const selectAccount = async (
1112
accounts: Array<Account>, { onCancel }: { onCancel: () => void }): Promise<Account> => {
@@ -30,8 +31,7 @@ export default class Login extends BaseCommand {
3031

3132
private _checkExistingCredentials = async () => {
3233
if (config.hasEnvVarsConfigured()) {
33-
this.warn('`CHECKLY_API_KEY` '
34-
+ 'or `CHECKLY_ACCOUNT_ID` environment variables are configured (via shell or .env file). You must delete them to use `npx checkly login`.')
34+
this.warn(`${commonMessages.envCredentialsConfigured} You must delete them to use \`npx checkly login\`.`)
3535
this.exit(0)
3636
}
3737

packages/cli/src/commands/logout.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,9 @@ export default class Logout extends BaseCommand {
3939

4040
config.clear()
4141
this.log('See you soon! 👋')
42+
43+
if (config.hasEnvVarsConfigured()) {
44+
this.warn(`${commonMessages.envCredentialsConfigured} You are still authenticated through them until you remove them.`)
45+
}
4246
}
4347
}

packages/cli/src/commands/whoami.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as api from '../rest/api.js'
2+
import config from '../services/config.js'
3+
import commonMessages from '../messages/common-messages.js'
24
import { AuthCommand } from './authCommand.js'
35

46
export default class Whoami extends AuthCommand {
@@ -18,5 +20,9 @@ export default class Whoami extends AuthCommand {
1820
if (addonNames.length > 0) {
1921
this.log(`Add-ons: ${addonNames.join(', ')}`)
2022
}
23+
if (config.hasEnvVarsConfigured()) {
24+
this.log()
25+
this.log(`This account is resolved from your environment, not a \`checkly login\` session. ${commonMessages.envCredentialsConfigured}`)
26+
}
2127
}
2228
}

packages/cli/src/messages/common-messages.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const commonMessages = {
22
forceMode: 'Force mode. Skips the confirmation dialog.',
33
configFile: 'The Checkly CLI configuration file. If not passed, uses the checkly.config.ts|js file in the current'
44
+ ' directory.',
5+
envCredentialsConfigured: '`CHECKLY_API_KEY` or `CHECKLY_ACCOUNT_ID` environment variables are configured'
6+
+ ' (via shell or .env file).',
57
}
68

79
export default commonMessages

0 commit comments

Comments
 (0)