/** * Authentication commands * @generated by @constructive-io/graphql-codegen * DO NOT EDIT - changes will be overwritten */ import { CLIOptions, Inquirerer, extractFirst } from 'inquirerer'; import { getStore } from '../executor'; const usage = '\ncsdk auth \n\nCommands:\n set-token Set API token for the current context\n status Show authentication status\n logout Remove credentials for the current context\n\nOptions:\n --context Specify context (defaults to current context)\n\n --help, -h Show this help message\n'; export default async ( argv: Partial>, prompter: Inquirerer, _options: CLIOptions ) => { if (argv.help || argv.h) { console.log(usage); process.exit(0); } const store = getStore(); const { first: subcommand, newArgv } = extractFirst(argv); if (!subcommand) { const answer = await prompter.prompt(argv, [ { type: 'autocomplete', name: 'subcommand', message: 'What do you want to do?', options: ['set-token', 'status', 'logout'], }, ]); return handleAuthSubcommand(answer.subcommand as string, newArgv, prompter, store); } return handleAuthSubcommand(subcommand, newArgv, prompter, store); }; async function handleAuthSubcommand( subcommand: string, argv: Partial>, prompter: Inquirerer, store: ReturnType ) { switch (subcommand) { case 'set-token': return handleSetToken(argv, prompter, store); case 'status': return handleStatus(store); case 'logout': return handleLogout(argv, prompter, store); default: console.log(usage); process.exit(1); } } async function handleSetToken( argv: Partial>, prompter: Inquirerer, store: ReturnType ) { const current = store.getCurrentContext(); if (!current) { console.error('No active context. Run "context create" first.'); process.exit(1); } const { first: token } = extractFirst(argv); let tokenValue = token; if (!tokenValue) { const answer = await prompter.prompt(argv, [ { type: 'password', name: 'token', message: 'API Token', required: true, }, ]); tokenValue = answer.token as string; } store.setCredentials(current.name, { token: String(tokenValue || '').trim(), }); console.log(`Token saved for context: ${current.name}`); } function handleStatus(store: ReturnType) { const contexts = store.listContexts(); const settings = store.loadSettings(); if (contexts.length === 0) { console.log('No contexts configured.'); return; } console.log('Authentication Status:'); for (const ctx of contexts) { const isCurrent = ctx.name === settings.currentContext; const hasAuth = store.hasValidCredentials(ctx.name); const marker = isCurrent ? '* ' : ' '; const status = hasAuth ? 'authenticated' : 'no token'; console.log(`${marker}${ctx.name} [${status}]`); } } async function handleLogout( argv: Partial>, prompter: Inquirerer, store: ReturnType ) { const current = store.getCurrentContext(); if (!current) { console.log('No active context.'); return; } const confirm = await prompter.prompt(argv, [ { type: 'confirm', name: 'confirm', message: `Remove credentials for "${current.name}"?`, default: false, }, ]); if (!(confirm.confirm as boolean)) { return; } if (store.removeCredentials(current.name)) { console.log(`Credentials removed for: ${current.name}`); } else { console.log(`No credentials found for: ${current.name}`); } }