* Handle plugin-based authentication flow. * Returns true if auth was handled, false if it should fall through to default handling.
(plugin: { auth: PluginAuth }, provider: string)
| 82 | * Returns true if auth was handled, false if it should fall through to default handling. |
| 83 | */ |
| 84 | async function handlePluginAuth(plugin: { auth: PluginAuth }, provider: string): Promise<boolean> { |
| 85 | const check = await checkExistingConnection(provider) |
| 86 | if (!check.proceed) { |
| 87 | prompts.outro("Cancelled") |
| 88 | return true |
| 89 | } |
| 90 | |
| 91 | let index = 0 |
| 92 | if (plugin.auth.methods.length > 1) { |
| 93 | const method = await prompts.select({ |
| 94 | message: "Login method", |
| 95 | options: [ |
| 96 | ...plugin.auth.methods.map((x, index) => ({ |
| 97 | label: x.label, |
| 98 | value: index.toString(), |
| 99 | })), |
| 100 | ], |
| 101 | }) |
| 102 | if (prompts.isCancel(method)) throw new UI.CancelledError() |
| 103 | index = parseInt(method) |
| 104 | } |
| 105 | const method = plugin.auth.methods[index] |
| 106 | |
| 107 | // Handle prompts for all auth types |
| 108 | await new Promise((resolve) => setTimeout(resolve, 10)) |
| 109 | const inputs: Record<string, string> = {} |
| 110 | if (method.prompts) { |
| 111 | for (const prompt of method.prompts) { |
| 112 | if (prompt.condition && !prompt.condition(inputs)) { |
| 113 | continue |
| 114 | } |
| 115 | if (prompt.type === "select") { |
| 116 | const value = await prompts.select({ |
| 117 | message: prompt.message, |
| 118 | options: prompt.options, |
| 119 | }) |
| 120 | if (prompts.isCancel(value)) throw new UI.CancelledError() |
| 121 | inputs[prompt.key] = value |
| 122 | } else { |
| 123 | const value = await prompts.text({ |
| 124 | message: prompt.message, |
| 125 | placeholder: prompt.placeholder, |
| 126 | validate: prompt.validate ? (v) => prompt.validate!(v ?? "") : undefined, |
| 127 | }) |
| 128 | if (prompts.isCancel(value)) throw new UI.CancelledError() |
| 129 | inputs[prompt.key] = value |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (method.type === "oauth") { |
| 135 | const authorize = await method.authorize(inputs) |
| 136 | |
| 137 | const persistAuthResult = async (result: Awaited<ReturnType<typeof authorize.callback>>) => { |
| 138 | if (!result || result.type !== "success") return |
| 139 | const saveProvider = result.provider ?? provider |
| 140 | |
| 141 | let authInfo: Auth.Info |
no test coverage detected