Encryption keys
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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,
|
||||
);
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user