diff --git a/src/lib/apis/auths/index.ts b/src/lib/apis/auths/index.ts index 169998726..a690a476b 100644 --- a/src/lib/apis/auths/index.ts +++ b/src/lib/apis/auths/index.ts @@ -318,3 +318,78 @@ export const updateJWTExpiresDuration = async (token: string, duration: string) return res; }; + +export const createApiKey = async (token: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err.detail; + return null; + }); + if (error) { + throw error; + } + return res; +} + +export const getApiKey = async (token: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { + method: 'GET', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err.detail; + return null; + }); + if (error) { + throw error; + } + return res; +} + +export const deleteApiKey = async (token: string) => { + let error = null; + + const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { + method: 'DELETE', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + } + }) + .then(async (res) => { + if (!res.ok) throw await res.json(); + return res.json(); + }) + .catch((err) => { + console.log(err); + error = err.detail; + return null; + }); + if (error) { + throw error; + } + return res; +} diff --git a/src/lib/components/chat/Settings/Account.svelte b/src/lib/components/chat/Settings/Account.svelte index 3a2259a79..fbb5e6289 100644 --- a/src/lib/components/chat/Settings/Account.svelte +++ b/src/lib/components/chat/Settings/Account.svelte @@ -3,7 +3,7 @@ import { onMount, getContext } from 'svelte'; import { user } from '$lib/stores'; - import { updateUserProfile } from '$lib/apis/auths'; + import { updateUserProfile, createApiKey } from '$lib/apis/auths'; import UpdatePassword from './Account/UpdatePassword.svelte'; import { getGravatarUrl } from '$lib/apis/utils'; @@ -17,6 +17,9 @@ let name = ''; let showJWTToken = false; let JWTTokenCopied = false; + let showApiKey= false; + let ApiKeyCopied = false; + let localApiKey = localStorage.apiKey; let profileImageInputElement: HTMLInputElement; const submitHandler = async () => { @@ -33,6 +36,18 @@ return false; }; + const createApiKeyHandler = async () => { + const apiKey = await createApiKey(localStorage.token); + if (apiKey) { + localApiKey = apiKey["api_key"]; + localStorage.apiKey = localApiKey; + toast.success($i18n.t('API Key created.')); + } else { + toast.error($i18n.t('Failed to create API Key.')); + + } + }; + onMount(() => { name = $user.name; profileImageUrl = $user.profile_image_url; @@ -170,41 +185,83 @@
-
-
-
{$i18n.t('JWT Token')}
-
+
+
+
+
{$i18n.t('JWT Token')}
+
-
-
- +
+
+ + + +
+
+
+
+
{$i18n.t('API Key')}
+
- + + + {#if showApiKey} + + + + + {:else} + + + + + {/if} + +
+ + +
diff --git a/src/routes/auth/+page.svelte b/src/routes/auth/+page.svelte index d392e9f77..22a77110f 100644 --- a/src/routes/auth/+page.svelte +++ b/src/routes/auth/+page.svelte @@ -1,6 +1,6 @@