From 7c5f6d71b3607e0529fd1359ac0346bdf5784a72 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Tue, 2 Apr 2024 09:39:55 -0700 Subject: [PATCH] refac --- src/lib/apis/auths/index.ts | 10 ++--- .../components/chat/Settings/Account.svelte | 44 +++++++++++-------- src/routes/auth/+page.svelte | 13 +----- 3 files changed, 31 insertions(+), 36 deletions(-) diff --git a/src/lib/apis/auths/index.ts b/src/lib/apis/auths/index.ts index b703e8746..548a9418d 100644 --- a/src/lib/apis/auths/index.ts +++ b/src/lib/apis/auths/index.ts @@ -319,7 +319,7 @@ export const updateJWTExpiresDuration = async (token: string, duration: string) return res; }; -export const createApiKey = async (token: string) => { +export const createAPIKey = async (token: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { @@ -341,10 +341,10 @@ export const createApiKey = async (token: string) => { if (error) { throw error; } - return res; + return res.api_key; }; -export const getApiKey = async (token: string) => { +export const getAPIKey = async (token: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { @@ -366,10 +366,10 @@ export const getApiKey = async (token: string) => { if (error) { throw error; } - return res; + return res.api_key; }; -export const deleteApiKey = async (token: string) => { +export const deleteAPIKey = async (token: string) => { let error = null; const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { diff --git a/src/lib/components/chat/Settings/Account.svelte b/src/lib/components/chat/Settings/Account.svelte index f368d90f9..916cd811a 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, createApiKey } from '$lib/apis/auths'; + import { updateUserProfile, createAPIKey, getAPIKey } from '$lib/apis/auths'; import UpdatePassword from './Account/UpdatePassword.svelte'; import { getGravatarUrl } from '$lib/apis/utils'; @@ -15,11 +15,14 @@ let profileImageUrl = ''; let name = ''; + let showJWTToken = false; let JWTTokenCopied = false; - let showApiKey = false; - let ApiKeyCopied = false; - let localApiKey = localStorage.apiKey; + + let APIKey = ''; + let showAPIKey = false; + let APIKeyCopied = false; + let profileImageInputElement: HTMLInputElement; const submitHandler = async () => { @@ -36,20 +39,23 @@ return false; }; - const createApiKeyHandler = async () => { - const apiKey = await createApiKey(localStorage.token); - if (apiKey) { - localApiKey = apiKey['api_key']; - localStorage.apiKey = localApiKey; + const createAPIKeyHandler = async () => { + APIKey = await createAPIKey(localStorage.token); + if (APIKey) { toast.success($i18n.t('API Key created.')); } else { toast.error($i18n.t('Failed to create API Key.')); } }; - onMount(() => { + onMount(async () => { name = $user.name; profileImageUrl = $user.profile_image_url; + + APIKey = await getAPIKey(localStorage.token).catch((error) => { + console.log(error); + return ''; + }); }); @@ -293,18 +299,18 @@