aboutsummaryrefslogtreecommitdiff
path: root/web/ui/src/routes/dashboard/keys/+page.server.ts
diff options
context:
space:
mode:
authorbenj <benj@rse8.com>2026-04-10 11:13:34 +0800
committerbenj <benj@rse8.com>2026-04-10 11:13:34 +0800
commit493746b14c1251a45b061d2e3edd9160c929d2b9 (patch)
tree1607cceb94c1aac1a17a01bb5c0d71b97342e892 /web/ui/src/routes/dashboard/keys/+page.server.ts
parentc041641634650c31e03c70dcad132fd94cb08e63 (diff)
downloadtidyindex-493746b14c1251a45b061d2e3edd9160c929d2b9.tar
tidyindex-493746b14c1251a45b061d2e3edd9160c929d2b9.tar.gz
tidyindex-493746b14c1251a45b061d2e3edd9160c929d2b9.tar.bz2
tidyindex-493746b14c1251a45b061d2e3edd9160c929d2b9.tar.lz
tidyindex-493746b14c1251a45b061d2e3edd9160c929d2b9.tar.xz
tidyindex-493746b14c1251a45b061d2e3edd9160c929d2b9.tar.zst
tidyindex-493746b14c1251a45b061d2e3edd9160c929d2b9.zip
a basic ui and landing web interface for tidyindex.com
Diffstat (limited to '')
-rw-r--r--web/ui/src/routes/dashboard/keys/+page.server.ts68
1 files changed, 68 insertions, 0 deletions
diff --git a/web/ui/src/routes/dashboard/keys/+page.server.ts b/web/ui/src/routes/dashboard/keys/+page.server.ts
new file mode 100644
index 0000000..5491283
--- /dev/null
+++ b/web/ui/src/routes/dashboard/keys/+page.server.ts
@@ -0,0 +1,68 @@
+import { fail, type Actions } from '@sveltejs/kit';
+
+import {
+ createKey,
+ listKeys,
+ revokeKey,
+ countActiveKeys
+} from '$lib/server/keys';
+import { PLANS } from '$lib/plans';
+import type { PageServerLoad } from './$types';
+
+export const load: PageServerLoad = async ({ locals }) => {
+ const account = locals.account!;
+ const keys = listKeys(account.id);
+ return {
+ keys,
+ activeCount: countActiveKeys(account.id),
+ plan: PLANS[account.plan]
+ };
+};
+
+export const actions: Actions = {
+ create: async ({ request, locals }) => {
+ const account = locals.account!;
+ const form = await request.formData();
+ const name = ((form.get('name') ?? '') as string).trim();
+ const scopes = form.getAll('scopes').map((s) => s.toString());
+
+ if (!name) {
+ return fail(400, { error: 'Give the key a name so you can recognize it later.' });
+ }
+
+ const plan = PLANS[account.plan];
+ const active = countActiveKeys(account.id);
+ if (Number.isFinite(plan.maxKeys) && active >= plan.maxKeys) {
+ return fail(403, {
+ error: `Your ${plan.name} plan allows ${plan.maxKeys} active key${
+ plan.maxKeys === 1 ? '' : 's'
+ }. Revoke one or upgrade your plan first.`
+ });
+ }
+
+ const created = createKey({
+ accountId: account.id,
+ name,
+ scopes
+ });
+
+ return {
+ created: {
+ id: created.id,
+ plaintext: created.plaintext,
+ name: created.name
+ }
+ };
+ },
+
+ revoke: async ({ request, locals }) => {
+ const account = locals.account!;
+ const form = await request.formData();
+ const id = (form.get('id') ?? '').toString();
+ if (!id) return fail(400, { error: 'Missing key id.' });
+
+ const ok = revokeKey(account.id, id);
+ if (!ok) return fail(404, { error: 'Key not found.' });
+ return { revokedId: id };
+ }
+};