import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
import { sqlite } from "https://esm.town/v/std/sqlite";
export default async function(req: Request): Promise<Response> {
const TABLE_NAME = "todepond_lab_login_users_with_times";
const body = await req.json();
const { username, password } = body;
const userQuery = await sqlite.execute({
sql: `SELECT * FROM ${TABLE_NAME} WHERE username = ?`,
args: [username],
});
if (userQuery.rows.length === 0) {
return new Response(JSON.stringify({ error: "user not found" }), { status: 404 });
}
const user = userQuery.rows[0];
const storedPassword = user[2];
const passwordMatch = await bcrypt.compare(password, storedPassword);
if (username === "TodePond") {
return new Response(JSON.stringify({ error: "cannot delete admin" }), { status: 400 });
}
if (!passwordMatch) {
return new Response(JSON.stringify({ error: "wrong password" }), { status: 401 });
}
if (user[5] === 1) {
return new Response(JSON.stringify({ error: "user is banned" }), { status: 403 });
}
await sqlite.execute({
sql: `DELETE FROM ${TABLE_NAME} WHERE username = ?`,
args: [username],
});
return new Response(JSON.stringify(user), { status: 200 });
}