From ee45deb43937cbf3996c3c379461110a484bd086 Mon Sep 17 00:00:00 2001 From: whoswhip Date: Fri, 13 Feb 2026 21:05:48 -0500 Subject: [PATCH] feat(frontend:api-keys): implement API key management features including creation, deletion, and fetching this also fixed a few layout issues and lint issues --- .../src/routes/user/[username]/+page.svelte | 287 +++++++++++++++++- 1 file changed, 276 insertions(+), 11 deletions(-) diff --git a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte index d8b9089..303052b 100644 --- a/SharpbinV3.Client/src/routes/user/[username]/+page.svelte +++ b/SharpbinV3.Client/src/routes/user/[username]/+page.svelte @@ -17,7 +17,9 @@ Flag, LoaderCircle, Check, - X + X, + Plus, + Copy } from '@lucide/svelte'; import { extractDateFromUUIDv7, @@ -33,6 +35,7 @@ import { onMount } from 'svelte'; import Paste from '$lib/components/Paste.svelte'; import { reportStatusLabels, reportTargetLabels, reportTypeLabels } from '$lib/types/report'; + import { fade } from 'svelte/transition'; export let data: PageData; @@ -53,6 +56,13 @@ let isEditingDisplayName = false; let editDisplayNameValue = ''; + let apiKeys: { uuid: string; name: string; createdAt: string; lastUsedAt: string | null }[] = []; + let newApiKeyName = ''; + let newApiKeyValue = ''; + let isCreatingApiKey = false; + let apiKeyError = ''; + let copiedApiKey = false; + $: isOwner = data.user && $user ? data.user?.uuid === $user.uuid : false; $: totpEnabled = Boolean($user?.totpEnabled); $: reportsSubmitted = data.reportsSubmitted; @@ -62,16 +72,26 @@ ? 'settings' : 'pastes'; - function updateTab(tab: typeof activeTab) { - activeTab = tab; + async function updateTab(newPageTab: typeof activeTab) { + if (activeTab === newPageTab) return; + activeTab = newPageTab; + const url = new URL(page.url); - url.searchParams.set('tab', tab); - goto(url, { replaceState: true, noScroll: true, keepFocus: true }); + url.searchParams.set('tab', newPageTab); + + await goto(url, { replaceState: true, noScroll: true, keepFocus: true, invalidateAll: false }); + + if (newPageTab === 'settings' && isOwner && apiKeys.length === 0) { + await fetchApiKeys(); + } } $: { const tab = page.url.searchParams.get('tab'); switch (tab) { + case 'pastes': + activeTab = 'pastes'; + break; case 'reportsSubmitted': if (reportsSubmitted) activeTab = 'reportsSubmitted'; else activeTab = 'pastes'; @@ -81,11 +101,18 @@ else activeTab = 'pastes'; break; case 'settings': - if (isOwner || $user?.roles?.some((r) => r === 255 || r === 1)) activeTab = 'settings'; - else activeTab = 'pastes'; + if (isOwner || $user?.roles?.some((r) => r === 255 || r === 1)) { + activeTab = 'settings'; + if (isOwner && apiKeys.length === 0) fetchApiKeys(); + } else activeTab = 'pastes'; break; default: - activeTab = 'pastes'; + if (isOwner) { + activeTab = 'settings'; + if (apiKeys.length === 0) fetchApiKeys(); + } else { + activeTab = 'pastes'; + } } } @@ -238,6 +265,95 @@ editDisplayNameValue = ''; } + async function fetchApiKeys() { + const token = getToken(); + if (!token) return; + if (apiKeys.length > 0) return; + loading = true; + try { + const res = await fetch('/api/auth/apikey/list', { + headers: { Authorization: `Bearer ${token}` } + }); + if (res.ok) { + const json = await res.json(); + apiKeys = Array.isArray(json.apiKeys) ? json.apiKeys : []; + } + } catch (e) { + console.error('Failed to fetch API keys', e); + } finally { + loading = false; + } + } + + async function createApiKey() { + if (!newApiKeyName.trim()) return; + loading = true; + apiKeyError = ''; + const token = getToken(); + try { + const res = await fetch('/api/auth/apikey/create', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${token}` + }, + body: JSON.stringify({ name: newApiKeyName.trim() }) + }); + const json = await res.json(); + if (res.ok) { + newApiKeyValue = json.apiKey.key; + apiKeys = [ + ...apiKeys, + { + uuid: json.apiKey.uuid, + name: json.apiKey.name, + createdAt: json.apiKey.createdAt, + lastUsedAt: null + } + ]; + newApiKeyName = ''; + isCreatingApiKey = false; + } else { + apiKeyError = json.message || 'Failed to create API key'; + } + } catch { + apiKeyError = 'Network error'; + } finally { + loading = false; + } + } + + async function deleteApiKey(uuid: string) { + const ok = await openModal({ + mode: 'confirm', + title: 'Delete API Key', + message: 'Are you sure you want to delete this API key? This action cannot be undone.', + confirmButtonText: 'Delete', + cancelValue: false + }); + if (!ok) return; + + loading = true; + apiKeyError = ''; + const token = getToken(); + try { + const res = await fetch(`/api/auth/apikey/${uuid}`, { + method: 'DELETE', + headers: { Authorization: `Bearer ${token}` } + }); + if (res.ok) { + apiKeys = apiKeys.filter((k) => k.uuid !== uuid); + } else { + const json = await res.json(); + apiKeyError = json.message || 'Failed to delete API key'; + } + } catch { + apiKeyError = 'Network error'; + } finally { + loading = false; + } + } + async function openEditRoles() { const value = await openModal({ mode: 'multiselect', @@ -278,6 +394,12 @@ interval = setInterval(() => { now = new Date(); }, 1000); + + const tab = page.url.searchParams.get('tab'); + if (tab === 'settings' && isOwner) { + fetchApiKeys(); + } + return () => { if (interval) clearInterval(interval); }; @@ -476,7 +598,7 @@ { if (e.key === 'Enter') saveDisplayName(); @@ -515,7 +637,7 @@ {#if isOwner} -
+
@@ -549,7 +671,7 @@ {/if} {#if $user?.roles?.some((r) => r === 255)} -
+
@@ -577,6 +699,149 @@
{/if} + {#if isOwner} +
+
+
+
+

API Keys

+

+ Manage API keys for accessing Sharpbin programmatically. +

+
+
+ + {#if apiKeyError} +
+ {apiKeyError} +
+ {/if} + +
+ {#if isCreatingApiKey} +
+ + + +
+ {:else} + + {/if} +
+ + {#if newApiKeyValue} +
+
+ API Key Created + +
+

+ Please copy your API key now. You won't be able to see it again. +

+
+ {newApiKeyValue} + +
+
+ {/if} + +
+ {#if apiKeys.length === 0} +

No API keys found.

+ {:else} + {#each apiKeys as key (key.uuid)} +
+
+ {key.name} +
+ Created: {new Date(key.createdAt).toLocaleDateString()} + Last used: {key.lastUsedAt + ? dateToRelativeString(new Date(key.lastUsedAt), true, false, now) + : 'Never'} +
+
+ +
+ {/each} + {/if} +
+
+
+ {/if} + {#if isOwner || $user?.roles?.some((r) => r === 255)}