feat(identity): personal API tokens, created and revoked from the dashboard, act as their owner
This commit is contained in:
1 parent
4d1d874676
commit
4dac44f98a
21 files changed
+2167
-1205
No files matched your search
@@ -0,0 +1,81 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from "vue";
|
||||
import { Plus } from "@lucide/vue";
|
||||
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
|
||||
interface PersonalToken {
|
||||
id: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
const tokens = ref<PersonalToken[]>([]);
|
||||
const created = ref("");
|
||||
const error = ref("");
|
||||
const busy = ref(false);
|
||||
|
||||
async function call<T>(path: string, init: RequestInit = {}): Promise<T | undefined> {
|
||||
const response = await fetch(path, {
|
||||
...init,
|
||||
headers: { Accept: "application/json", "X-Archie-CSRF": "1" },
|
||||
});
|
||||
if (!response.ok) throw new Error((await response.text()).trim());
|
||||
return response.status === 204 ? undefined : ((await response.json()) as T);
|
||||
}
|
||||
|
||||
async function run(action: () => Promise<void>): Promise<void> {
|
||||
busy.value = true;
|
||||
try {
|
||||
await action();
|
||||
error.value = "";
|
||||
} catch (cause) {
|
||||
error.value = String((cause as Error).message || cause);
|
||||
} finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
const load = () =>
|
||||
run(async () => {
|
||||
tokens.value = (await call<{ tokens: PersonalToken[] }>("/api/tokens"))?.tokens ?? [];
|
||||
});
|
||||
|
||||
const create = () =>
|
||||
run(async () => {
|
||||
created.value = (await call<{ token: string }>("/api/tokens", { method: "POST" }))?.token ?? "";
|
||||
await load();
|
||||
});
|
||||
|
||||
const revoke = (id: string) =>
|
||||
run(async () => {
|
||||
await call(`/api/tokens/${id}`, { method: "DELETE" });
|
||||
await load();
|
||||
});
|
||||
|
||||
onMounted(load);
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="mb-8" aria-labelledby="personal-tokens">
|
||||
<div class="mb-3 flex items-center justify-between gap-2">
|
||||
<h2 id="personal-tokens" class="text-sm font-medium">Your API tokens</h2>
|
||||
<Button size="sm" :disabled="busy" @click="create"><Plus data-icon="inline-start" /> New token</Button>
|
||||
</div>
|
||||
<p class="mb-3 text-xs text-fg-subtle">
|
||||
A token acts as you. Send it as <code>Authorization: Bearer <token></code>.
|
||||
</p>
|
||||
<p v-if="error" role="alert" class="mb-3 text-sm text-danger">{{ error }}</p>
|
||||
<div v-if="created" class="mb-3 rounded-lg border border-border bg-card p-3">
|
||||
<p class="mb-2 text-xs">Copy this token now. It is not shown again.</p>
|
||||
<Input :model-value="created" readonly aria-label="New token" @focus="($event.target as HTMLInputElement).select()" />
|
||||
</div>
|
||||
<ul v-if="tokens.length" class="divide-y divide-border rounded-lg border border-border bg-card" aria-label="API tokens">
|
||||
<li v-for="token in tokens" :key="token.id" class="flex items-center gap-3 px-4 py-2.5">
|
||||
<code class="flex-1 truncate text-xs">{{ token.id.slice(0, 12) }}</code>
|
||||
<span class="text-xs text-fg-subtle">{{ new Date(token.created_at).toLocaleString() }}</span>
|
||||
<Button variant="ghost" size="sm" class="text-danger" :disabled="busy" @click="revoke(token.id)">Revoke</Button>
|
||||
</li>
|
||||
</ul>
|
||||
</section>
|
||||
</template>
|
||||
@@ -3,6 +3,7 @@ import { ref } from "vue";
|
||||
import { Bot, KeyRound, Plus, ShieldCheck, User } from "@lucide/vue";
|
||||
|
||||
import PageHeader from "@/base/PageHeader.vue";
|
||||
import PersonalTokensCard from "@/settings/PersonalTokensCard.vue";
|
||||
import {
|
||||
AlertDialog,
|
||||
AlertDialogAction,
|
||||
@@ -67,6 +68,8 @@ const lifecycle = {
|
||||
<div>
|
||||
<PageHeader title="Identities" />
|
||||
|
||||
<PersonalTokensCard />
|
||||
|
||||
<p v-if="store.error" role="alert" class="mb-4 text-sm text-danger">{{ store.error }}</p>
|
||||
|
||||
<form class="mb-6 flex flex-wrap gap-2" @submit.prevent="create">
|
||||
|
||||
Reference in new issue
Block a user