test+fix auth endpoint
This commit is contained in:
23
.bruno/auth user.bru
Normal file
23
.bruno/auth user.bru
Normal file
@@ -0,0 +1,23 @@
|
||||
meta {
|
||||
name: auth user
|
||||
type: http
|
||||
seq: 2
|
||||
}
|
||||
|
||||
post {
|
||||
url: http://localhost:14014/api/auth
|
||||
body: json
|
||||
auth: inherit
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"name": "teest",
|
||||
"pin": "1234"
|
||||
}
|
||||
}
|
||||
|
||||
settings {
|
||||
encodeUrl: true
|
||||
timeout: 0
|
||||
}
|
||||
16
mod.ts
16
mod.ts
@@ -33,17 +33,17 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
||||
if (body.pin.length < 4 || body.pin.length > 20) return genericResponse(STATUS_CODE.BadRequest, `PIN too ${body.pin.length < 4 ? 'short' : 'long'}.`);
|
||||
if (body.email.length > 20) return genericResponse(STATUS_CODE.BadRequest, `Email too long.`);
|
||||
|
||||
const userId = nanoid();
|
||||
const id = nanoid();
|
||||
|
||||
let writeFailure = false;
|
||||
await dbClient.execute('INSERT INTO users(id,name,pin,email) values(?,?,?,?)', [userId, body.name, body.pin, body.email]).catch(() => {
|
||||
await dbClient.execute('INSERT INTO users(id,name,pin,email) values(?,?,?,?)', [id, body.name, body.pin, body.email]).catch(() => {
|
||||
writeFailure = true;
|
||||
});
|
||||
|
||||
if (writeFailure) {
|
||||
return genericResponse(STATUS_CODE.InternalServerError, "Couldn't write DB.");
|
||||
} else {
|
||||
return genericResponse(STATUS_CODE.OK, JSON.stringify({ userId }));
|
||||
return genericResponse(STATUS_CODE.OK, JSON.stringify({ id }));
|
||||
}
|
||||
} else {
|
||||
return genericResponse(STATUS_CODE.BadRequest, 'Username Taken.');
|
||||
@@ -52,23 +52,27 @@ Deno.serve({ port: config.api.port }, async (req) => {
|
||||
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(() => {
|
||||
const loginMatch = await dbClient.query('SELECT id, email, deleteCode 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 id = loginMatch[0].id;
|
||||
const email = loginMatch[0].email;
|
||||
const deleteCode = loginMatch[0].deleteCode;
|
||||
|
||||
switch (req.method) {
|
||||
case 'POST':
|
||||
if (path === '/auth' || path === '/auth/') {
|
||||
return genericResponse(STATUS_CODE.OK, JSON.stringify({ userId, hasEmail: email.length > 0 }));
|
||||
return genericResponse(STATUS_CODE.OK, JSON.stringify({ id, hasEmail: email.length > 0 }));
|
||||
}
|
||||
break;
|
||||
case 'PUT':
|
||||
break;
|
||||
case 'DELETE':
|
||||
if (path === '/unenroll' || '/unenroll/') {
|
||||
//
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user