* Check if provider already has credentials BEFORE starting auth flow. * Returns whether to proceed with auth and optionally a connection name.
(provider: string)
| 20 | * Returns whether to proceed with auth and optionally a connection name. |
| 21 | */ |
| 22 | async function checkExistingConnection(provider: string): Promise<{ proceed: boolean; connectionName?: string }> { |
| 23 | const connections = await Auth.listConnections(provider) |
| 24 | |
| 25 | if (connections.length === 0) { |
| 26 | return { proceed: true } |
| 27 | } |
| 28 | |
| 29 | const existingInfo = connections[0].info |
| 30 | let existingLabel = "" |
| 31 | |
| 32 | if (existingInfo.type === "codex" && existingInfo.email) { |
| 33 | existingLabel = existingInfo.email |
| 34 | } else if (existingInfo.type === "oauth") { |
| 35 | existingLabel = "OAuth" |
| 36 | } else if (existingInfo.type === "api") { |
| 37 | existingLabel = "API key" |
| 38 | } else if (existingInfo.type === "github") { |
| 39 | existingLabel = "GitHub token" |
| 40 | } |
| 41 | |
| 42 | prompts.log.warn(`You already have a connection for ${provider}${existingLabel ? ` (${existingLabel})` : ""}`) |
| 43 | |
| 44 | const addAnother = await prompts.confirm({ |
| 45 | message: "Do you want to add another connection?", |
| 46 | initialValue: true, |
| 47 | }) |
| 48 | |
| 49 | if (prompts.isCancel(addAnother)) return { proceed: false } |
| 50 | |
| 51 | if (!addAnother) { |
| 52 | const overwrite = await prompts.confirm({ |
| 53 | message: "Do you want to overwrite the existing connection?", |
| 54 | initialValue: false, |
| 55 | }) |
| 56 | |
| 57 | if (prompts.isCancel(overwrite)) return { proceed: false } |
| 58 | |
| 59 | if (!overwrite) { |
| 60 | return { proceed: false } |
| 61 | } |
| 62 | |
| 63 | return { proceed: true } |
| 64 | } |
| 65 | |
| 66 | const connectionName = await prompts.text({ |
| 67 | message: "Name for this connection", |
| 68 | placeholder: "mycompany", |
| 69 | validate: (value) => { |
| 70 | if (!value) return "Connection name is required" |
| 71 | return Auth.validateConnectionName(value) |
| 72 | }, |
| 73 | }) |
| 74 | |
| 75 | if (prompts.isCancel(connectionName)) return { proceed: false } |
| 76 | |
| 77 | return { proceed: true, connectionName } |
| 78 | } |
| 79 |
no test coverage detected