import { parseArgs } from "@std/cli/parse-args"; import { performDeviceFlow } from "../auth/device_flow.ts"; import { ConfigManager } from "../auth/config.ts"; function showLoginHelp() { console.log(` slices login - Authenticate with Slices using device code flow USAGE: slices login [OPTIONS] OPTIONS: --aip-url AIP server base URL (default: https://auth.slices.network) --client-id OAuth client ID (default: 24e77d48-a892-4043-b113-ea241f339397) --scope OAuth scope (default: atproto transition:generic repo:*) --force Force login even if already authenticated -h, --help Show this help message EXAMPLES: slices login slices login --aip-url https://custom-aip.example.com slices login --force `); } export async function loginCommand( commandArgs: unknown[], _globalArgs: Record ): Promise { const args = parseArgs(commandArgs as string[], { boolean: ["help", "force"], string: ["aip-url", "client-id", "scope"], alias: { h: "help", }, }); if (args.help) { showLoginHelp(); return; } const config = new ConfigManager(); await config.load(); // Check if already authenticated if (!args.force && config.isAuthenticated()) { const authConfig = config.get().auth!; console.log(`Already authenticated as ${authConfig.did}`); console.log("Use --force to login again"); return; } const aipBaseUrl = args["aip-url"] as string; const clientId = args["client-id"] as string; const scope = args.scope as string; if (args.force && config.isAuthenticated()) { await config.logout(); } await performDeviceFlow(aipBaseUrl, clientId, scope); }