feat: support set api key from client side

This commit is contained in:
Fred Liang 2023-12-24 03:44:13 +08:00
parent 4169431f2c
commit 20f2f61349
No known key found for this signature in database
GPG Key ID: 4DABDA85EF70EC71
5 changed files with 16 additions and 4 deletions

View File

@ -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 = {

View File

@ -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 = {

View File

@ -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}

View File

@ -13,7 +13,7 @@ const cn = {
Auth: {
Title: "需要密码",
Tips: "管理员开启了密码验证,请在下方填入访问码",
SubTips: "或者输入你的 OpenAI API 密钥",
SubTips: "或者输入你的 OpenAI 或 Google API 密钥",
Input: "在此处填写访问码",
Confirm: "确认",
Later: "稍后再说",

View File

@ -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",