add auth endpoint and name+pin validation

This commit is contained in:
Ean Milligan
2026-04-08 01:17:03 -04:00
parent 64d70d6d67
commit 3336d1ba7c

25
mod.ts
View File

@@ -19,7 +19,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
if (req.method === 'GET') {
// handle all gets
} else if (req.method === 'POST' && path === '/enroll') {
} else if (req.method === 'POST' && (path === '/enroll' || path === '/enroll/')) {
const body = await req.json();
let readFailure = false;
@@ -49,7 +49,28 @@ Deno.serve({ port: config.api.port }, async (req) => {
return genericResponse(STATUS_CODE.BadRequest, 'Username Taken.');
}
} else {
// handle auth then all other shiz
const body = await req.json();
let readFailure = false;
const loginMatch = await dbClient.query('SELECT id, email FROM users WHERE name = ? AND pin = ?', [body.name, body.pin]).catch(() => {
readFailure = true;
});
if (readFailure) return genericResponse(STATUS_CODE.InternalServerError, "Couldn't read DB.");
if (loginMatch.length === 0) return genericResponse(STATUS_CODE.Forbidden, 'Invalid name/PIN combination.');
const userId = loginMatch[0].userId;
const email = loginMatch[0].email;
switch (req.method) {
case 'POST':
if (path === '/auth' || path === '/auth/') {
return genericResponse(STATUS_CODE.OK, JSON.stringify({ userId, hasEmail: email.length > 0 }));
}
break;
case 'PUT':
break;
case 'DELETE':
break;
}
}
return genericResponse(STATUS_CODE.NotImplemented);