add auth endpoint and name+pin validation
This commit is contained in:
25
mod.ts
25
mod.ts
@@ -19,7 +19,7 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
|
|
||||||
if (req.method === 'GET') {
|
if (req.method === 'GET') {
|
||||||
// handle all gets
|
// handle all gets
|
||||||
} else if (req.method === 'POST' && path === '/enroll') {
|
} else if (req.method === 'POST' && (path === '/enroll' || path === '/enroll/')) {
|
||||||
const body = await req.json();
|
const body = await req.json();
|
||||||
|
|
||||||
let readFailure = false;
|
let readFailure = false;
|
||||||
@@ -49,7 +49,28 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
|||||||
return genericResponse(STATUS_CODE.BadRequest, 'Username Taken.');
|
return genericResponse(STATUS_CODE.BadRequest, 'Username Taken.');
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
return genericResponse(STATUS_CODE.NotImplemented);
|
||||||
|
|||||||
Reference in New Issue
Block a user