Skip to content

Commit 64f7e71

Browse files
committed
feat(auth): 支持在控制台登录时自动处理 API 密钥
1 parent 993972c commit 64f7e71

2 files changed

Lines changed: 14 additions & 8 deletions

File tree

packages/cli/src/commands/auth/login-console.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ function openInBrowser(url: string): Promise<void> {
278278

279279
export async function runConsoleLogin(
280280
consoleOrigin: string,
281-
opts?: { needApiKey?: boolean },
281+
opts?: { needApiKey?: boolean; onApiKey?: (key: string) => Promise<void> },
282282
): Promise<void> {
283283
const state = randomBytes(16).toString("hex");
284284
const server = http.createServer(async (req, res) => {
@@ -304,12 +304,15 @@ export async function runConsoleLogin(
304304

305305
if (accessToken || apiKey) {
306306
try {
307-
const existing = readConfigFile() as Record<string, unknown>;
308-
if (accessToken) existing.access_token = accessToken;
309-
if (apiKey) existing.api_key = apiKey;
310-
await writeConfigFile(existing);
311-
if (accessToken) process.stderr.write(`access_token saved to ${getConfigPath()}\n`);
312-
if (apiKey) process.stderr.write(`api_key saved to ${getConfigPath()}\n`);
307+
if (accessToken) {
308+
const existing = readConfigFile() as Record<string, unknown>;
309+
existing.access_token = accessToken;
310+
await writeConfigFile(existing);
311+
process.stderr.write(`access_token saved to ${getConfigPath()}\n`);
312+
}
313+
if (apiKey && opts?.onApiKey) {
314+
await opts.onApiKey(apiKey);
315+
}
313316
} catch {
314317
res.writeHead(500, { "Content-Type": "text/plain; charset=utf-8" });
315318
res.end("Failed to save credentials\n");

packages/cli/src/commands/auth/login.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ export default defineCommand({
5858
return;
5959
}
6060
const hasApiKey = !!(config.apiKey || config.fileApiKey);
61-
await runConsoleLogin(resolveConsoleOrigin(), { needApiKey: !hasApiKey });
61+
await runConsoleLogin(resolveConsoleOrigin(), {
62+
needApiKey: !hasApiKey,
63+
onApiKey: (key) => validateKeyAndPersist(config, key),
64+
});
6265
return;
6366
}
6467

0 commit comments

Comments
 (0)