Admin page

This commit is contained in:
2026-10-29 21:42:26 -03:00
parent 07e61d9dd1
commit f7f34e1a57
18 changed files with 369 additions and 13 deletions
+4
View File
@@ -1,4 +1,5 @@
import { define } from "@/utils.ts";
import AdminButton from "@/islands/AdminButton.tsx";
export default define.page(function ({ Component }) {
return (
@@ -11,6 +12,9 @@ export default define.page(function ({ Component }) {
<body class="flex min-h-screen flex-col items-center bg-white font-sans text-gray-900 md:bg-gray-100 dark:bg-black dark:text-gray-100">
<header class="flex h-16 w-full flex-none flex-row items-center px-6 md:h-24 md:px-8">
<a href="/" class="text-2xl font-bold md:text-3xl">vces</a>
<div class="ml-auto">
<AdminButton />
</div>
</header>
<main class="flex w-full max-w-5xl md:m-auto md:w-[calc(100%-3rem)] md:flex-1 md:items-center md:py-8">
<section class="w-full bg-white p-6 md:min-h-[42rem] md:rounded-xl md:p-8 md:shadow-big dark:bg-gray-900 md:dark:border md:dark:border-gray-800">
+6
View File
@@ -0,0 +1,6 @@
import { define } from "@/utils.ts";
import AdminPanel from "@/islands/AdminPanel.tsx";
export default define.page(function () {
return <AdminPanel />;
});
+77
View File
@@ -0,0 +1,77 @@
import { z } from "@zod/zod";
import { define } from "@/utils.ts";
import type { AdminUserEntry, CreatedUser } from "@/src/admin.ts";
import { authenticate, forbidden, unauthorized } from "@/src/auth.ts";
import { generate_code } from "@/src/crypto.ts";
import { db, type FileRow, type User } from "@/src/db.ts";
// Sent by the Form island, so the checkbox is "on" or missing
const CreateUserBody = z.object({
name: z.string().trim().min(1).max(100),
admin: z.literal("on").optional(),
});
async function authenticate_admin(req: Request) {
const user = await authenticate(req);
if (!user) {
return unauthorized();
}
if ((user as Partial<User>).admin !== true) {
return forbidden();
}
return undefined;
}
export const handler = define.handlers({
async GET(ctx) {
const denied = await authenticate_admin(ctx.req);
if (denied) {
return denied;
}
const files = await db.get_all_entries("files") as FileRow[];
const usage = new Map<string, { file_count: number; storage_used: number }>();
for (const file of files) {
const entry = usage.get(file.user_id) ?? { file_count: 0, storage_used: 0 };
entry.file_count++;
entry.storage_used += file.size;
usage.set(file.user_id, entry);
}
const users = await db.get_all_entries("users") as (User & { id: string; date_created: string })[];
return Response.json(
users.map((user): AdminUserEntry => ({
id: user.id,
name: user.name,
admin: user.admin,
date_created: user.date_created,
has_key: Boolean(user.key_check),
...(usage.get(user.id) ?? { file_count: 0, storage_used: 0 }),
})),
);
},
async POST(ctx) {
const denied = await authenticate_admin(ctx.req);
if (denied) {
return denied;
}
const body = CreateUserBody.safeParse(await ctx.req.json().catch(() => null));
if (!body.success) {
return Response.json({ error: "Enter a name" }, { status: 400 });
}
let code = generate_code();
while (await db.get_entry_by_index("users", "code", code)) {
code = generate_code();
}
const user = await db.create_entry("users", { name: body.data.name, code, admin: body.data.admin === "on" });
return Response.json(
{ id: user.id, name: user.name, admin: user.admin, code: user.code } satisfies CreatedUser,
{ status: 201 },
);
},
});
+16
View File
@@ -0,0 +1,16 @@
import { define } from "@/utils.ts";
import type { MeResponse } from "@/src/admin.ts";
import { authenticate, unauthorized } from "@/src/auth.ts";
import type { User } from "@/src/db.ts";
export const handler = define.handlers({
async GET(ctx) {
const user = await authenticate(ctx.req);
if (!user) {
return unauthorized();
}
const { name, admin } = user as Partial<User>;
return Response.json({ id: user.id, name: name ?? "", admin: admin === true } satisfies MeResponse);
},
});