From da830c58f3d0029c8b63a6659fbc261e1aa0e21b Mon Sep 17 00:00:00 2001 From: HarshIyer Date: Fri, 24 May 2024 23:39:27 +0530 Subject: [PATCH] Add create endpoint --- src/handlers/create.ts | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/handlers/create.ts b/src/handlers/create.ts index 30ab1bc..826a878 100644 --- a/src/handlers/create.ts +++ b/src/handlers/create.ts @@ -1,32 +1,30 @@ import { Context } from 'hono'; import { HTTPException } from 'hono/http-exception'; -import jwt from '@tsndr/cloudflare-worker-jwt'; -async function createJWT(email: string) { - const token = await jwt.sign({ email }, 'secret'); - return token; -} export default async function create(c: Context) { let body; - let email: string | undefined, otp: string; - const emailPattern = /^[a-zA-Z]+\d{2}[a-zA-Z]{3}\d{1,3}@iiitkottayam\.ac\.in$/; - const otpPattern = /^\d{6}$/; try { body = await c.req.json(); - email = body.email; - otp = body.otp; } catch (e) { throw new HTTPException(400); } - if (!otp || !otpPattern.test(otp) || !email || !emailPattern.test(email)) { + if (!body.email || !body.token || !body.keyHash || !body.aes256Bit || !body.salt) { throw new HTTPException(401); } - + //Maybe add some way of token verification later on + const emailPattern = /^[a-zA-Z]+\d{2}[a-zA-Z]{3}\d{1,3}@iiitkottayam\.ac\.in$/; + if (!emailPattern.test(body.email)) { + throw new HTTPException(401); + } + const stmt = `INSERT INTO users (email, token, keyHash, aes256Bit, salt) VALUES ($1, $2, $3, $4, $5)`; + const values = [body.email, body.token, body.keyHash, body.aes256Bit, body.salt]; + try { + await c.env.DB.prepare.query(stmt, values); + } catch (e) { + throw new HTTPException(500); + } return c.json({ status: 'success', - data: { - token: await createJWT(email), - }, }); }