This commit is contained in:
Timothy J. Baek 2024-04-02 09:39:55 -07:00
parent e49e04c56a
commit 7c5f6d71b3
3 changed files with 31 additions and 36 deletions

View File

@ -319,7 +319,7 @@ export const updateJWTExpiresDuration = async (token: string, duration: string)
return res; return res;
}; };
export const createApiKey = async (token: string) => { export const createAPIKey = async (token: string) => {
let error = null; let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
@ -341,10 +341,10 @@ export const createApiKey = async (token: string) => {
if (error) { if (error) {
throw error; throw error;
} }
return res; return res.api_key;
}; };
export const getApiKey = async (token: string) => { export const getAPIKey = async (token: string) => {
let error = null; let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {
@ -366,10 +366,10 @@ export const getApiKey = async (token: string) => {
if (error) { if (error) {
throw error; throw error;
} }
return res; return res.api_key;
}; };
export const deleteApiKey = async (token: string) => { export const deleteAPIKey = async (token: string) => {
let error = null; let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, { const res = await fetch(`${WEBUI_API_BASE_URL}/auths/api_key`, {

View File

@ -3,7 +3,7 @@
import { onMount, getContext } from 'svelte'; import { onMount, getContext } from 'svelte';
import { user } from '$lib/stores'; 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 UpdatePassword from './Account/UpdatePassword.svelte';
import { getGravatarUrl } from '$lib/apis/utils'; import { getGravatarUrl } from '$lib/apis/utils';
@ -15,11 +15,14 @@
let profileImageUrl = ''; let profileImageUrl = '';
let name = ''; let name = '';
let showJWTToken = false; let showJWTToken = false;
let JWTTokenCopied = false; let JWTTokenCopied = false;
let showApiKey = false;
let ApiKeyCopied = false; let APIKey = '';
let localApiKey = localStorage.apiKey; let showAPIKey = false;
let APIKeyCopied = false;
let profileImageInputElement: HTMLInputElement; let profileImageInputElement: HTMLInputElement;
const submitHandler = async () => { const submitHandler = async () => {
@ -36,20 +39,23 @@
return false; return false;
}; };
const createApiKeyHandler = async () => { const createAPIKeyHandler = async () => {
const apiKey = await createApiKey(localStorage.token); APIKey = await createAPIKey(localStorage.token);
if (apiKey) { if (APIKey) {
localApiKey = apiKey['api_key'];
localStorage.apiKey = localApiKey;
toast.success($i18n.t('API Key created.')); toast.success($i18n.t('API Key created.'));
} else { } else {
toast.error($i18n.t('Failed to create API Key.')); toast.error($i18n.t('Failed to create API Key.'));
} }
}; };
onMount(() => { onMount(async () => {
name = $user.name; name = $user.name;
profileImageUrl = $user.profile_image_url; profileImageUrl = $user.profile_image_url;
APIKey = await getAPIKey(localStorage.token).catch((error) => {
console.log(error);
return '';
});
}); });
</script> </script>
@ -293,18 +299,18 @@
<div class="flex w-full"> <div class="flex w-full">
<input <input
class="w-full rounded-l-lg py-1.5 pl-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none" class="w-full rounded-l-lg py-1.5 pl-4 text-sm dark:text-gray-300 dark:bg-gray-800 outline-none"
type={showApiKey ? 'text' : 'password'} type={showAPIKey ? 'text' : 'password'}
value={localApiKey} value={APIKey}
disabled disabled
/> />
<button <button
class="px-2 transition rounded-r-lg dark:bg-gray-800" class="px-2 transition rounded-r-lg dark:bg-gray-800"
on:click={() => { on:click={() => {
showApiKey = !showApiKey; showAPIKey = !showAPIKey;
}} }}
> >
{#if showApiKey} {#if showAPIKey}
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 16 16" viewBox="0 0 16 16"
@ -341,14 +347,14 @@
<button <button
class="ml-1.5 px-1.5 py-1 hover:bg-gray-800 transition rounded-lg" class="ml-1.5 px-1.5 py-1 hover:bg-gray-800 transition rounded-lg"
on:click={() => { on:click={() => {
copyToClipboard(localApiKey); copyToClipboard(APIKey);
ApiKeyCopied = true; APIKeyCopied = true;
setTimeout(() => { setTimeout(() => {
ApiKeyCopied = false; APIKeyCopied = false;
}, 2000); }, 2000);
}} }}
> >
{#if ApiKeyCopied} {#if APIKeyCopied}
<svg <svg
xmlns="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20" viewBox="0 0 20 20"
@ -385,7 +391,7 @@
<button <button
class=" px-1.5 py-1 hover:bg-gray-800 transition rounded-lg" class=" px-1.5 py-1 hover:bg-gray-800 transition rounded-lg"
on:click={() => { on:click={() => {
createApiKeyHandler(); createAPIKeyHandler();
}} }}
> >
<svg <svg

View File

@ -1,6 +1,6 @@
<script> <script>
import { goto } from '$app/navigation'; import { goto } from '$app/navigation';
import { userSignIn, userSignUp, getApiKey } from '$lib/apis/auths'; import { userSignIn, userSignUp } from '$lib/apis/auths';
import Spinner from '$lib/components/common/Spinner.svelte'; import Spinner from '$lib/components/common/Spinner.svelte';
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants'; import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
import { WEBUI_NAME, config, user } from '$lib/stores'; import { WEBUI_NAME, config, user } from '$lib/stores';
@ -27,17 +27,6 @@
} }
}; };
const setApiKey = async (token) => {
const apiKey = await getApiKey(token).catch((error) => {
toast.error(error);
return null;
});
if (apiKey) {
localStorage.apiKey = apiKey['api-key'];
}
};
const signInHandler = async () => { const signInHandler = async () => {
const sessionUser = await userSignIn(email, password).catch((error) => { const sessionUser = await userSignIn(email, password).catch((error) => {
toast.error(error); toast.error(error);