1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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 = "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);
// dont delete todepond
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 });
}
// is the user banned?
if (user[5] === 1) {
return new Response(JSON.stringify({ error: "user is banned" }), { status: 403 });
}
// delete the user
await sqlite.execute({
sql: `DELETE FROM ${TABLE_NAME} WHERE username = ?`,
args: [username],
});
return new Response(JSON.stringify(user), { status: 200 });
}