1- import { stat } from 'node:fs/promises' ;
1+ import { stat , readFile } from 'node:fs/promises' ;
22import * as path from 'node:path' ;
33import { homedir } from 'node:os' ;
44
@@ -42,6 +42,36 @@ export function getCredentialsPath(configDir: string): string {
4242 return path . join ( configDir , '.credentials.json' ) ;
4343}
4444
45+ /**
46+ * Gets the path to the settings file
47+ */
48+ export function getSettingsPath ( configDir : string ) : string {
49+ return path . join ( configDir , 'settings.json' ) ;
50+ }
51+
52+ /**
53+ * Check if settings file contains auth environment variables
54+ */
55+ async function hasAuthInSettings ( settingsPath : string ) : Promise < boolean > {
56+ try {
57+ const content = await readFile ( settingsPath , 'utf-8' ) ;
58+ const settings = JSON . parse ( content ) ;
59+
60+ // Check if settings has an env object with auth keys
61+ if ( settings . env && typeof settings . env === 'object' ) {
62+ const authKeys = [ 'ANTHROPIC_API_KEY' , 'ANTHROPIC_AUTH_TOKEN' ] ;
63+ for ( const key of authKeys ) {
64+ if ( settings . env [ key ] ) {
65+ return true ;
66+ }
67+ }
68+ }
69+ return false ;
70+ } catch {
71+ return false ;
72+ }
73+ }
74+
4575/**
4676 * Gets paths to all Claude-related files that need to be cleaned up
4777 */
@@ -65,12 +95,21 @@ export async function isAuthenticated(options?: ClaudeAuthOptions): Promise<bool
6595 const configDir = resolveClaudeConfigDir ( options ) ;
6696 const credPath = getCredentialsPath ( configDir ) ;
6797
98+ // Check if credentials file exists
6899 try {
69100 await stat ( credPath ) ;
70101 return true ;
71102 } catch {
72- return false ;
103+ // Credentials file doesn't exist, check settings.json for auth env vars
73104 }
105+
106+ // Check if settings.json has auth env vars
107+ const settingsPath = getSettingsPath ( configDir ) ;
108+ if ( await hasAuthInSettings ( settingsPath ) ) {
109+ return true ;
110+ }
111+
112+ return false ;
74113}
75114
76115/**
@@ -165,14 +204,20 @@ export async function ensureAuth(options?: ClaudeAuthOptions): Promise<boolean>
165204 const configDir = resolveClaudeConfigDir ( options ) ;
166205 const credPath = getCredentialsPath ( configDir ) ;
167206
168- // If already authenticated, nothing to do
207+ // If already authenticated via credentials file , nothing to do
169208 try {
170209 await stat ( credPath ) ;
171210 return true ;
172211 } catch {
173212 // Credentials file doesn't exist
174213 }
175214
215+ // Check if settings.json has auth env vars
216+ const settingsPath = getSettingsPath ( configDir ) ;
217+ if ( await hasAuthInSettings ( settingsPath ) ) {
218+ return true ;
219+ }
220+
176221 // Check if CLI is installed
177222 const cliInstalled = await checkCliInstalled ( metadata . cliBinary ) ;
178223 if ( ! cliInstalled ) {
0 commit comments