MCPcopy Create free account
hub / github.com/tiann/hapi / handleAuthCommand

Function handleAuthCommand

cli/src/commands/auth.ts:10–87  ·  view source on GitHub ↗
(args: string[])

Source from the content-addressed store, hash-verified

8import type { CommandDefinition } from './types'
9
10export async function handleAuthCommand(args: string[]): Promise<void> {
11 const subcommand = args[0]
12
13 if (!subcommand || subcommand === 'help' || subcommand === '--help' || subcommand === '-h') {
14 showHelp()
15 return
16 }
17
18 if (subcommand === 'status') {
19 await initializeApiUrl()
20 const settings = await readSettings()
21 const envToken = process.env.CLI_API_TOKEN
22 const settingsToken = settings.cliApiToken
23 const hasToken = Boolean(envToken || settingsToken)
24 const tokenSource = envToken ? 'environment' : (settingsToken ? 'settings file' : 'none')
25 console.log(chalk.bold('\nDirect Connect Status\n'))
26 console.log(chalk.gray(` HAPI_API_URL: ${configuration.apiUrl}`))
27 console.log(chalk.gray(` CLI_API_TOKEN: ${hasToken ? 'set' : 'missing'}`))
28 console.log(chalk.gray(` Token Source: ${tokenSource}`))
29 console.log(chalk.gray(` Machine ID: ${settings.machineId ?? 'not set'}`))
30 console.log(chalk.gray(` Host: ${os.hostname()}`))
31
32 if (!hasToken) {
33 console.log('')
34 console.log(chalk.yellow(' Token not configured. To get your token:'))
35 console.log(chalk.gray(' 1. Check the server startup logs (first run shows generated token)'))
36 console.log(chalk.gray(' 2. Read ~/.hapi/settings.json on the server'))
37 console.log(chalk.gray(' 3. Ask your server administrator (if token is set via env var)'))
38 console.log('')
39 console.log(chalk.gray(' Then run: hapi auth login'))
40 }
41 return
42 }
43
44 if (subcommand === 'login') {
45 if (!process.stdin.isTTY) {
46 console.error(chalk.red('Cannot prompt for token in non-TTY environment.'))
47 console.error(chalk.gray('Set CLI_API_TOKEN environment variable instead.'))
48 process.exit(1)
49 }
50
51 const rl = readline.createInterface({ input, output })
52
53 try {
54 const token = await rl.question(chalk.cyan('Enter CLI_API_TOKEN: '))
55
56 if (!token.trim()) {
57 console.error(chalk.red('Token cannot be empty'))
58 process.exit(1)
59 }
60
61 await updateSettings(current => ({
62 ...current,
63 cliApiToken: token.trim()
64 }))
65 configuration._setCliApiToken(token.trim())
66 console.log(chalk.green(`\nToken saved to ${configuration.settingsFile}`))
67 } finally {

Callers 2

auth.test.tsFile · 0.90
auth.tsFile · 0.85

Calls 8

initializeApiUrlFunction · 0.90
readSettingsFunction · 0.90
updateSettingsFunction · 0.90
clearMachineIdFunction · 0.90
showHelpFunction · 0.85
errorMethod · 0.80
closeMethod · 0.65
_setCliApiTokenMethod · 0.45

Tested by

no test coverage detected