From 20f2f61349d68b3720623d37803fe968868c834a Mon Sep 17 00:00:00 2001
From: Fred Liang <info@fredliang.cn>
Date: Sun, 24 Dec 2023 03:44:13 +0800
Subject: [PATCH] feat: support set api key from client side

---
 app/api/google/[...path]/route.ts |  4 +++-
 app/client/platforms/google.ts    |  1 -
 app/components/auth.tsx           | 11 +++++++++++
 app/locales/cn.ts                 |  2 +-
 app/locales/en.ts                 |  2 +-
 5 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/app/api/google/[...path]/route.ts b/app/api/google/[...path]/route.ts
index ec5d65523..95b1c6418 100644
--- a/app/api/google/[...path]/route.ts
+++ b/app/api/google/[...path]/route.ts
@@ -43,7 +43,9 @@ async function handle(
     10 * 60 * 1000,
   );
 
-  const key = req.nextUrl.searchParams.get("key") ?? serverConfig.googleApiKey;
+  const bearToken = req.headers.get("Authorization") ?? "";
+  const token = bearToken.trim().replaceAll("Bearer ", "").trim();
+  const key = token ?? serverConfig.googleApiKey;
   const fetchUrl = `${baseUrl}/${path}?key=${key}`;
 
   const fetchOptions: RequestInit = {
diff --git a/app/client/platforms/google.ts b/app/client/platforms/google.ts
index b44def99f..edff13a2b 100644
--- a/app/client/platforms/google.ts
+++ b/app/client/platforms/google.ts
@@ -49,7 +49,6 @@ export class GeminiApi implements LLMApi {
     const shouldStream = false;
     const controller = new AbortController();
     options.onController?.(controller);
-
     try {
       const chatPath = this.path(Google.ChatPath);
       const chatPayload = {
diff --git a/app/components/auth.tsx b/app/components/auth.tsx
index 7962d46be..57118349b 100644
--- a/app/components/auth.tsx
+++ b/app/components/auth.tsx
@@ -64,6 +64,17 @@ export function AuthPage() {
               );
             }}
           />
+          <input
+            className={styles["auth-input"]}
+            type="password"
+            placeholder={Locale.Settings.Access.Google.ApiKey.Placeholder}
+            value={accessStore.googleApiKey}
+            onChange={(e) => {
+              accessStore.update(
+                (access) => (access.googleApiKey = e.currentTarget.value),
+              );
+            }}
+          />
         </>
       ) : null}
 
diff --git a/app/locales/cn.ts b/app/locales/cn.ts
index 42270b2fb..69b6c025f 100644
--- a/app/locales/cn.ts
+++ b/app/locales/cn.ts
@@ -13,7 +13,7 @@ const cn = {
   Auth: {
     Title: "需要密码",
     Tips: "管理员开启了密码验证,请在下方填入访问码",
-    SubTips: "或者输入你的 OpenAI API 密钥",
+    SubTips: "或者输入你的 OpenAI 或 Google API 密钥",
     Input: "在此处填写访问码",
     Confirm: "确认",
     Later: "稍后再说",
diff --git a/app/locales/en.ts b/app/locales/en.ts
index 4d437ffd8..21aa63833 100644
--- a/app/locales/en.ts
+++ b/app/locales/en.ts
@@ -15,7 +15,7 @@ const en: LocaleType = {
   Auth: {
     Title: "Need Access Code",
     Tips: "Please enter access code below",
-    SubTips: "Or enter your OpenAI API Key",
+    SubTips: "Or enter your OpenAI or Google API Key",
     Input: "access code",
     Confirm: "Confirm",
     Later: "Later",