| 1 | import { select } from "@clack/prompts"; |
| 2 | import { getOAuthHandle, getOAuthSession } from "./oauth-store"; |
| 3 | import { getCredentials } from "./credentials"; |
| 4 | import type { Credentials } from "./types"; |
| 5 | import { exitOnCancel } from "./prompts"; |
| 6 | |
| 7 | /** |
| 8 | * Prompt user to select from multiple credentials |
| 9 | */ |
| 10 | export async function selectCredential( |
| 11 | allCredentials: Array<{ id: string; type: "app-password" | "oauth" }>, |
| 12 | ): Promise<Credentials | null> { |
| 13 | // Build options with friendly labels |
| 14 | const options = await Promise.all( |
| 15 | allCredentials.map(async ({ id, type }) => { |
| 16 | let label = id; |
| 17 | if (type === "oauth") { |
| 18 | const handle = await getOAuthHandle(id); |
| 19 | label = handle ? `${handle} (${id})` : id; |
| 20 | } |
| 21 | return { |
| 22 | value: { id, type }, |
| 23 | label: `${label} [${type}]`, |
| 24 | }; |
| 25 | }), |
| 26 | ); |
| 27 | |
| 28 | const selected = exitOnCancel( |
| 29 | await select({ |
| 30 | message: "Multiple identities found. Select one:", |
| 31 | options, |
| 32 | }), |
| 33 | ); |
| 34 | |
| 35 | // Load the full credentials for the selected identity |
| 36 | if (selected.type === "oauth") { |
| 37 | const session = await getOAuthSession(selected.id); |
| 38 | if (session) { |
| 39 | const handle = await getOAuthHandle(selected.id); |
| 40 | return { |
| 41 | type: "oauth", |
| 42 | did: selected.id, |
| 43 | handle: handle || selected.id, |
| 44 | }; |
| 45 | } |
| 46 | } else { |
| 47 | const creds = await getCredentials(selected.id); |
| 48 | if (creds) { |
| 49 | return creds; |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return null; |
| 54 | } |