Skip to content

Commit

Permalink
Add verification method
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshIyer committed May 25, 2024
1 parent bb77180 commit 7f66348
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/handlers/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,28 @@ export default async function create(c: Context) {
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$/;

let emailPattern = /^[a-zA-Z]+\d{2}[a-zA-Z]{3}\d{1,3}@iiitkottayam\.ac\.in$/;

Check failure on line 15 in src/handlers/create.ts

View workflow job for this annotation

GitHub Actions / lint

'emailPattern' is never reassigned. Use 'const' instead
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];

let userToken = body.token;

Check failure on line 20 in src/handlers/create.ts

View workflow job for this annotation

GitHub Actions / lint

'userToken' is never reassigned. Use 'const' instead
let token_stmt = `SELECT email FROM users WHERE token = $1`;

Check failure on line 21 in src/handlers/create.ts

View workflow job for this annotation

GitHub Actions / lint

'token_stmt' is never reassigned. Use 'const' instead
let token_values = [userToken];

Check failure on line 22 in src/handlers/create.ts

View workflow job for this annotation

GitHub Actions / lint

'token_values' is never reassigned. Use 'const' instead
let token_email;

try {
let res = await c.env.DB.prepare.query(token_stmt, token_values);

Check failure on line 26 in src/handlers/create.ts

View workflow job for this annotation

GitHub Actions / lint

'res' is never reassigned. Use 'const' instead
token_email = res.rows[0].email;
} catch (e) {
throw new HTTPException(500);
}
if (token_email !== body.email) {
throw new HTTPException(401);
}
let stmt = `INSERT INTO users (email, token, keyHash, aes256Bit, salt) VALUES ($1, $2, $3, $4, $5)`;

Check failure on line 34 in src/handlers/create.ts

View workflow job for this annotation

GitHub Actions / lint

'stmt' is never reassigned. Use 'const' instead
let values = [body.email, body.token, body.keyHash, body.aes256Bit, body.salt];

Check failure on line 35 in src/handlers/create.ts

View workflow job for this annotation

GitHub Actions / lint

'values' is never reassigned. Use 'const' instead
try {
await c.env.DB.prepare.query(stmt, values);
} catch (e) {
Expand Down
12 changes: 10 additions & 2 deletions src/handlers/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,19 @@ export default async function verify(c: Context) {
if (!otp || !otpPattern.test(otp) || !email || !emailPattern.test(email)) {
throw new HTTPException(401);
}

const token = await createJWT(email);
const stmt = `INSERT INTO users (email, token) VALUES ($1, $2)`;
const values = [email, token];
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),
token: token,
},
});
}

0 comments on commit 7f66348

Please sign in to comment.