From fc4fe211994312cf947f69745dd6e09335b04c85 Mon Sep 17 00:00:00 2001 From: Heiner Lohaus Date: Sat, 7 Dec 2024 03:01:43 +0100 Subject: Add Authentication Setup Guide --- g4f/gui/client/static/js/chat.v1.js | 50 ++++++++++++++++++++++++------------- g4f/gui/server/backend.py | 2 +- 2 files changed, 33 insertions(+), 19 deletions(-) (limited to 'g4f/gui') diff --git a/g4f/gui/client/static/js/chat.v1.js b/g4f/gui/client/static/js/chat.v1.js index 834a5de9..cb0706eb 100644 --- a/g4f/gui/client/static/js/chat.v1.js +++ b/g4f/gui/client/static/js/chat.v1.js @@ -744,7 +744,11 @@ const delete_conversation = async (conversation_id) => { }; const set_conversation = async (conversation_id) => { - history.pushState({}, null, `/chat/${conversation_id}`); + try { + history.pushState({}, null, `/chat/${conversation_id}`); + } catch (e) { + console.error(e); + } window.conversation_id = conversation_id; await clear_conversation(); @@ -898,7 +902,11 @@ async function add_conversation(conversation_id, content) { items: [], }); } - history.pushState({}, null, `/chat/${conversation_id}`); + try { + history.pushState({}, null, `/chat/${conversation_id}`); + } catch (e) { + console.error(e); + } } async function save_system_message() { @@ -1287,23 +1295,29 @@ async function on_api() { register_settings_storage(); - models = await api("models"); - models.forEach((model) => { - let option = document.createElement("option"); - option.value = option.text = model; - modelSelect.appendChild(option); - }); - - providers = await api("providers") - Object.entries(providers).forEach(([provider, label]) => { - let option = document.createElement("option"); - option.value = provider; - option.text = label; - providerSelect.appendChild(option); - }) + try { + models = await api("models"); + models.forEach((model) => { + let option = document.createElement("option"); + option.value = option.text = model; + modelSelect.appendChild(option); + }); + providers = await api("providers") + Object.entries(providers).forEach(([provider, label]) => { + let option = document.createElement("option"); + option.value = provider; + option.text = label; + providerSelect.appendChild(option); + }); + await load_provider_models(appStorage.getItem("provider")); + } catch (e) { + console.error(e) + if (document.location.pathname == "/chat/") { + document.location.href = `/chat/error`; + } + } await load_settings_storage() - await load_provider_models(appStorage.getItem("provider")); const hide_systemPrompt = document.getElementById("hide-systemPrompt") const slide_systemPrompt_icon = document.querySelector(".slide-systemPrompt i"); @@ -1465,7 +1479,7 @@ async function api(ressource, args=null, file=null, message_id=null) { const url = `/backend-api/v2/${ressource}`; const headers = {}; if (api_key) { - headers.authorization = `Bearer ${api_key}`; + headers.x_api_key = api_key; } if (ressource == "conversation") { let body = JSON.stringify(args); diff --git a/g4f/gui/server/backend.py b/g4f/gui/server/backend.py index 3c87b0e2..a6179885 100644 --- a/g4f/gui/server/backend.py +++ b/g4f/gui/server/backend.py @@ -153,7 +153,7 @@ class Backend_Api(Api): return response def get_provider_models(self, provider: str): - api_key = None if request.authorization is None else request.authorization.token + api_key = request.headers.get("x_api_key") models = super().get_provider_models(provider, api_key) if models is None: return "Provider not found", 404 -- cgit v1.2.3