Skip to content

Commit

Permalink
Add create endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshIyer committed May 24, 2024
1 parent 84b1b08 commit da830c5
Showing 1 changed file with 13 additions and 15 deletions.
28 changes: 13 additions & 15 deletions src/handlers/create.ts
Original file line number Diff line number Diff line change
@@ -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),
},
});
}

0 comments on commit da830c5

Please sign in to comment.