feat(identity): name, lifetime and last-used on personal API tokens

This commit is contained in:
sam committed 2026-10-27 15:37:44 +11:00
1 parent 3210e30ce4
commit 91be2114cf
23 files changed
+449 -99

No files matched your search

+26 -4
View File
@@ -5,20 +5,33 @@ import { Copy, Plus, X } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
import { api } from "@/lib/api";
import { Section } from "@/components/ui/section";
import { Timestamp } from "@/components/ui/timestamp";
interface PersonalToken {
id: string;
name: string;
created_at: string;
expires_at: string | null;
last_used_at: string | null;
}
const lifetimes = [
{ days: 7, label: "7 days" },
{ days: 30, label: "30 days" },
{ days: 90, label: "90 days" },
{ days: 0, label: "No expiry" },
];
const available = ref(false);
const tokens = ref<PersonalToken[]>([]);
const created = ref("");
const error = ref("");
const busy = ref(false);
const name = ref("");
const days = ref(30);
async function run(action: () => Promise<void>): Promise<void> {
busy.value = true;
@@ -41,7 +54,8 @@ const load = () =>
const create = () =>
run(async () => {
created.value = (await api.createToken())?.token ?? "";
created.value = (await api.createToken(name.value.trim(), Number(days.value)))?.token ?? "";
name.value = "";
await load();
});
@@ -59,7 +73,13 @@ onMounted(load);
<template>
<Section title="API tokens">
<template v-if="available" #action>
<Button size="sm" :disabled="busy" @click="create"><Plus data-icon="inline-start" />New token</Button>
<form class="flex items-center gap-2" @submit.prevent="create">
<Input v-model="name" class="w-40" maxlength="40" required placeholder="Name" aria-label="Token name" />
<NativeSelect v-model="days" size="sm" aria-label="Lifetime">
<NativeSelectOption v-for="option in lifetimes" :key="option.days" :value="option.days">{{ option.label }}</NativeSelectOption>
</NativeSelect>
<Button type="submit" size="sm" :disabled="busy || !name.trim()"><Plus data-icon="inline-start" />New token</Button>
</form>
</template>
<p v-if="!available && !busy && !error" class="text-fg-subtle">Personal tokens need a signed-in person.</p>
<p v-if="error" role="alert" class="mb-3 text-danger">{{ error }}</p>
@@ -74,8 +94,10 @@ onMounted(load);
<p v-if="available && !tokens.length && !busy && !error" class="text-fg-subtle">No tokens yet.</p>
<ItemGroup v-if="tokens.length" aria-label="API tokens">
<Item v-for="token in tokens" :key="token.id" as="li" variant="outline" size="xs">
<code class="flex-1 truncate text-xs">{{ token.id.slice(0, 12) }}</code>
<span class="text-xs text-fg-subtle"><Timestamp :at="token.created_at" /></span>
<span class="flex-1 truncate text-sm">{{ token.name || token.id.slice(0, 12) }}</span>
<span class="text-xs text-fg-subtle">Created <Timestamp :at="token.created_at" /></span>
<span class="text-xs text-fg-subtle">Expires <Timestamp v-if="token.expires_at" :at="token.expires_at" /><template v-else>Never</template></span>
<span class="text-xs text-fg-subtle">Last used <Timestamp v-if="token.last_used_at" :at="token.last_used_at" /><template v-else>Never</template></span>
<Button variant="ghost" size="sm" class="text-danger" :disabled="busy" @click="revoke(token.id)">Revoke</Button>
</Item>
</ItemGroup>
+1 -1
View File
@@ -164,7 +164,7 @@ export const api = {
authentication: <T = unknown>() => request<T>("/api/authentication"),
saveAuthentication: (body: unknown) => request<void>("/api/authentication",{method:"PUT",body,parse:false}),
tokens: <T = unknown>() => request<T>("/api/tokens"),
createToken: () => request<{token:string}>("/api/tokens",{method:"POST",body:{}}),
createToken: (name:string,days:number) => request<{token:string}>("/api/tokens",{method:"POST",body:{name,days}}),
revokeToken: (id:string) => request<void>(`/api/tokens/${encodeURIComponent(id)}`,{method:"DELETE",body:{},parse:false}),
passkeys: () => request<{passkeys:{id:string;name:string}[]}>("/api/account/passkeys"),
deletePasskey: (id:string) => request<void>(`/api/account/passkeys/${encodeURIComponent(id)}`,{method:"DELETE",body:{},parse:false}),