Feature: API Tokens (#848)

* initial commit

* fix last_used timestamp

---------

Co-authored-by: Christian Beutel <>
This commit is contained in:
Flomp
2026-03-07 10:10:25 +01:00
committed by GitHub
parent ba88d08078
commit 711a592038
26 changed files with 781 additions and 8 deletions

View File

@@ -0,0 +1,59 @@
import { get } from "svelte/store";
import { currentUser } from "./user_store";
import type { APIToken } from "$lib/models/api_token";
import { APIError } from "$lib/util/api_util";
import type { ListResult } from "pocketbase";
export async function api_tokens_index(f: (url: RequestInfo | URL, config?: RequestInit) => Promise<Response> = fetch) {
const r = await f(`/api/v1/api-token`, {
method: 'GET',
})
if (!r.ok) {
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
const fetchedTokens: ListResult<APIToken> = await r.json();
return fetchedTokens;
}
export async function api_tokens_create(apiToken: APIToken) {
const user = get(currentUser)
if (!user) {
throw Error("Unauthenticated")
}
apiToken.user = user.id
let r = await fetch('/api/v1/api-token', {
method: 'PUT',
body: JSON.stringify(apiToken),
})
if (!r.ok) {
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
const model: APIToken & { rawToken: string } = await r.json();
return model;
}
export async function api_tokens_delete(token: APIToken) {
const r = await fetch('/api/v1/api-token/' + token.id, {
method: 'DELETE',
})
if (!r.ok) {
const response = await r.json();
throw new APIError(r.status, response.message, response.detail)
}
return await r.json();
}