33 lines
876 B
TypeScript
33 lines
876 B
TypeScript
import { z } from "@zod/zod";
|
|
|
|
import { define } from "@/utils.ts";
|
|
import { db, type User } from "@/src/db.ts";
|
|
import type { GetKeyResponse } from "@/src/keys.ts";
|
|
|
|
const GetKeyBody = z.object({
|
|
code: z.string().min(1),
|
|
});
|
|
|
|
export const handler = define.handlers({
|
|
async POST(ctx) {
|
|
const body = GetKeyBody.safeParse(await ctx.req.json().catch(() => null));
|
|
if (!body.success) {
|
|
return Response.json({ error: "Invalid request" }, { status: 400 });
|
|
}
|
|
|
|
const user = await db.get_entry_by_index("users", "code", body.data.code);
|
|
if (!user) {
|
|
return Response.json({ error: "Invalid code" }, { status: 401 });
|
|
}
|
|
|
|
const { key_check, passphrase_backup } = user as Partial<User>;
|
|
return Response.json(
|
|
{
|
|
user_id: user.id,
|
|
key_check: key_check ?? null,
|
|
passphrase_backup: passphrase_backup ?? null,
|
|
} satisfies GetKeyResponse,
|
|
);
|
|
},
|
|
});
|