From 3c6f4254eb3403ef61d9bf248d75a31efd1ea03f Mon Sep 17 00:00:00 2001 From: Qin Guan Date: Tue, 19 Sep 2023 20:33:42 +0800 Subject: [PATCH] feat: verify user --- server/api/auth/verify.post.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 server/api/auth/verify.post.ts diff --git a/server/api/auth/verify.post.ts b/server/api/auth/verify.post.ts new file mode 100644 index 0000000..d504ffa --- /dev/null +++ b/server/api/auth/verify.post.ts @@ -0,0 +1,32 @@ +import { z } from 'zod' + +const verifyRequestBody = z.object({ + email: z.string().email(), +}) + +export default defineCachedEventHandler(async (event) => { + const result = await verifyRequestBody.safeParseAsync(await readBody(event)) + if (!result.success) { + throw createError({ + status: 400, + statusMessage: 'Bad request', + }) + } + + const { data } = result + + const user = await event.context.database.query.users.findFirst({ + where: (users, { eq }) => eq(users.email, data.email), + }) + + if (user === null) { + throw createError({ + status: 404, + statusMessage: 'Not found', + }) + } + + return { ok: true } +}, { + maxAge: 60 * 60 * 24, +})