diff --git a/packages/ai/src/utils/oauth/anthropic.ts b/packages/ai/src/utils/oauth/anthropic.ts
index 687cfa3ca..48032f1e4 100644
--- a/packages/ai/src/utils/oauth/anthropic.ts
+++ b/packages/ai/src/utils/oauth/anthropic.ts
@@ -6,6 +6,7 @@
*/
import type { Server } from "node:http";
+import { oauthErrorHtml, oauthSuccessHtml } from "./oauth-page.js";
import { generatePKCE } from "./pkce.js";
import type { OAuthCredentials, OAuthLoginCallbacks, OAuthPrompt, OAuthProviderInterface } from "./types.js";
@@ -33,18 +34,6 @@ const CALLBACK_PATH = "/callback";
const REDIRECT_URI = `http://localhost:${CALLBACK_PORT}${CALLBACK_PATH}`;
const SCOPES =
"org:create_api_key user:profile user:inference user:sessions:claude_code user:mcp_servers user:file_upload";
-const SUCCESS_HTML = `
-
-
-
-
- Authentication successful
-
-
- Authentication successful. Return to your terminal to continue.
-
-`;
-
async function getNodeApis(): Promise {
if (nodeApis) return nodeApis;
if (!nodeApisPromise) {
@@ -110,15 +99,22 @@ async function startCallbackServer(expectedState: string): Promise {
- let result: { code: string; state: string } | null = null;
- let cancelled = false;
+ let settleWait: ((value: { code: string; state: string } | null) => void) | undefined;
+ const waitForCodePromise = new Promise<{ code: string; state: string } | null>((resolveWait) => {
+ let settled = false;
+ settleWait = (value) => {
+ if (settled) return;
+ settled = true;
+ resolveWait(value);
+ };
+ });
const server = createServer((req, res) => {
try {
const url = new URL(req.url || "", "http://localhost");
if (url.pathname !== CALLBACK_PATH) {
- res.writeHead(404, { "Content-Type": "text/plain; charset=utf-8" });
- res.end("Not found");
+ res.writeHead(404, { "Content-Type": "text/html; charset=utf-8" });
+ res.end(oauthErrorHtml("Callback route not found."));
return;
}
@@ -128,27 +124,25 @@ async function startCallbackServer(expectedState: string): PromiseAuthentication Failed
Error: ${error}