@@ -3,7 +3,13 @@ import chalk from 'chalk'
33import { ensureInteractive , prompt , promptHidden } from '../utils/prompt'
44import { openBrowser } from '../utils/browser'
55import { twirlLoader } from '../utils/misc'
6- import { saveCredentials , clearCredentials , type CredentialSource } from '../utils/credentials'
6+ import {
7+ saveCredentials ,
8+ clearCredentials ,
9+ loadCredentialsFromKeyring ,
10+ loadCredentialsFromFile ,
11+ type CredentialSource ,
12+ } from '../utils/credentials'
713import { createApi } from '../api'
814import {
915 checkTenant ,
@@ -25,8 +31,8 @@ async function resolveTenantUrl(): Promise<string> {
2531 }
2632
2733 try {
28- const result = await checkTenant ( teamName )
29- return result . redirectUrl
34+ const { tenantUrl } = await checkTenant ( teamName )
35+ return tenantUrl
3036 } catch ( e ) {
3137 const message = e instanceof Error ? e . message : String ( e )
3238 console . error ( chalk . red ( 'Error:' ) + ` Could not find team "${ teamName } ": ${ message } ` )
@@ -46,7 +52,7 @@ async function validateApiKey(tenantUrl: string, apiKey: string): Promise<boolea
4652
4753async function handleApiKeyLogin ( ) : Promise < void > {
4854 const tenantUrl = await resolveTenantUrl ( )
49- const apiKey = await promptHidden ( ' API Key: ' )
55+ const apiKey = await promptHidden ( ` API Key ${ chalk . gray ( '(Input Hidden)' ) } : ` )
5056 if ( ! apiKey ) {
5157 console . error ( chalk . red ( 'Error:' ) + ' API key is required.' )
5258 process . exit ( 1 )
@@ -156,10 +162,26 @@ const sourceLabels: Partial<Record<CredentialSource, string>> = {
156162 '.env' : 'a .env file in the current directory' ,
157163 '.qaspherecli' : 'a .qaspherecli file' ,
158164}
165+ async function findClearableSource ( ) : Promise < 'keyring' | 'credentials.json' | null > {
166+ if ( await loadCredentialsFromKeyring ( ) ) return 'keyring'
167+ if ( loadCredentialsFromFile ( ) ) return 'credentials.json'
168+ return null
169+ }
170+
159171async function handleLogout ( ) : Promise < void > {
160- const result = await clearCredentials ( )
172+ const clearable = await findClearableSource ( )
173+
174+ if ( clearable ) {
175+ try {
176+ await clearCredentials ( clearable )
177+ } catch ( e ) {
178+ const message = e instanceof Error ? e . message : String ( e )
179+ console . error (
180+ chalk . red ( 'Error:' ) + ` Could not clear credentials from ${ clearable } : ${ message } `
181+ )
182+ process . exit ( 1 )
183+ }
161184
162- if ( result . cleared ) {
163185 console . log ( 'Logged out.' )
164186
165187 // Warn if credentials are still available from another source
@@ -175,7 +197,7 @@ async function handleLogout(): Promise<void> {
175197 return
176198 }
177199
178- // Check if credentials come from a non-clearable source
200+ // No clearable source — check if credentials come from a non-clearable source
179201 const source = await resolveCredentialSource ( )
180202 if ( source ) {
181203 const label = sourceLabels [ source . source ] || source . source
0 commit comments