Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
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
43
44
45
46
47
48
import * as bcrypt from "https://deno.land/x/bcrypt@v0.4.1/mod.ts";
import { email } from "https://esm.town/v/std/email";
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, status } = 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 (!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 });
}
if (status.length > 9999) {
return new Response(JSON.stringify({ error: "status too long" }), { status: 400 });
}
const updatedUser = await sqlite.execute({
sql: `UPDATE ${TABLE_NAME} SET status = ?, last_updated = CURRENT_TIMESTAMP WHERE username = ?`,
args: [status, username],
});
/*email({
to: "todepond@gmail.com",
from: "todepond.com@valtown.email",
subject: "New Login status",
text: status,
});*/
return new Response(JSON.stringify(updatedUser.rows[0]), { status: 200 });
}
netux-todepondlabloginupdatestatus.web.val.run
August 30, 2024