File size: 1,765 Bytes
064bfd6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | import axios from 'axios'
import { getOauthConfig } from '../../constants/oauth.js'
import { getGlobalConfig, saveGlobalConfig } from '../../utils/config.js'
import { getAuthHeaders } from '../../utils/http.js'
import { logError } from '../../utils/log.js'
import { getClaudeCodeUserAgent } from '../../utils/userAgent.js'
/**
* Fetch the user's first Claude Code token date and store in config.
* This is called after successful login to cache when they started using Claude Code.
*/
export async function fetchAndStoreClaudeCodeFirstTokenDate(): Promise<void> {
try {
const config = getGlobalConfig()
if (config.claudeCodeFirstTokenDate !== undefined) {
return
}
const authHeaders = getAuthHeaders()
if (authHeaders.error) {
logError(new Error(`Failed to get auth headers: ${authHeaders.error}`))
return
}
const oauthConfig = getOauthConfig()
const url = `${oauthConfig.BASE_API_URL}/api/organization/claude_code_first_token_date`
const response = await axios.get(url, {
headers: {
...authHeaders.headers,
'User-Agent': getClaudeCodeUserAgent(),
},
timeout: 10000,
})
const firstTokenDate = response.data?.first_token_date ?? null
// Validate the date if it's not null
if (firstTokenDate !== null) {
const dateTime = new Date(firstTokenDate).getTime()
if (isNaN(dateTime)) {
logError(
new Error(
`Received invalid first_token_date from API: ${firstTokenDate}`,
),
)
// Don't save invalid dates
return
}
}
saveGlobalConfig(current => ({
...current,
claudeCodeFirstTokenDate: firstTokenDate,
}))
} catch (error) {
logError(error)
}
}
|