From 93ab1a90ef42b86ce5effe894f17a7e6b5e60064 Mon Sep 17 00:00:00 2001 From: Fred Date: Wed, 7 Feb 2024 13:06:46 +0800 Subject: [PATCH] fix: not using header authorization for google api --- app/client/api.ts | 25 ++++++++++++++----------- next.config.mjs | 11 ++++++++++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/app/client/api.ts b/app/client/api.ts index 69eb183bfc1..4b39fbfaed2 100644 --- a/app/client/api.ts +++ b/app/client/api.ts @@ -155,20 +155,23 @@ export function getHeaders() { : isAzure ? accessStore.azureApiKey : accessStore.openaiApiKey; - + const clientConfig = getClientConfig(); const makeBearer = (s: string) => `${isAzure ? "" : "Bearer "}${s.trim()}`; const validString = (x: string) => x && x.length > 0; - // use user's api key first - if (validString(apiKey)) { - headers[authHeader] = makeBearer(apiKey); - } else if ( - accessStore.enabledAccessControl() && - validString(accessStore.accessCode) - ) { - headers[authHeader] = makeBearer( - ACCESS_CODE_PREFIX + accessStore.accessCode, - ); + // when using google api in app, not set auth header + if (!(isGoogle && clientConfig?.isApp)) { + // use user's api key first + if (validString(apiKey)) { + headers[authHeader] = makeBearer(apiKey); + } else if ( + accessStore.enabledAccessControl() && + validString(accessStore.accessCode) + ) { + headers[authHeader] = makeBearer( + ACCESS_CODE_PREFIX + accessStore.accessCode, + ); + } } return headers; diff --git a/next.config.mjs b/next.config.mjs index 4faa63e5450..ae94f489545 100644 --- a/next.config.mjs +++ b/next.config.mjs @@ -64,8 +64,17 @@ if (mode !== "export") { nextConfig.rewrites = async () => { const ret = [ + // adjust for previous verison directly using "/api/proxy/" as proxy base route { - source: "/api/proxy/:path*", + source: "/api/proxy/v1/:path*", + destination: "https://api.openai.com/v1/:path*", + }, + { + source: "/api/proxy/google/:path*", + destination: "https://generativelanguage.googleapis.com/:path*", + }, + { + source: "/api/proxy/openai/:path*", destination: "https://api.openai.com/:path*", }, {