aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarvin Borner2020-07-18 23:48:04 +0200
committerMarvin Borner2020-07-18 23:48:04 +0200
commit781f0baee0a6514fd57b4f964344febd32bc45d1 (patch)
treedbdd62e12c897f14fa6271a4f8fa3cd8763bdd8a
parentaccb147b54bc99e5cd20059be5cb333031f15885 (diff)
Fiiiiiixxxxx! (or sth)
Co-authored-by: LarsVomMars <lars@kroenner.eu>
-rwxr-xr-xrun6
-rw-r--r--src/db/DBController.ts10
-rw-r--r--src/db/tables.sql4
-rw-r--r--src/db/user.ts23
-rw-r--r--src/handler/user.ts22
5 files changed, 39 insertions, 26 deletions
diff --git a/run b/run
index bfe033a..9362e9a 100755
--- a/run
+++ b/run
@@ -3,11 +3,13 @@
ARGS=(--allow-net --allow-env --allow-read --allow-write --unstable)
if grep -q "DEBUG=1" .env; then
- deno test "${ARGS[@]}" test/ &&
+ deno lint --unstable &&
+ deno test "${ARGS[@]}" test/ &&
echo "Tests succeeded!" &&
deno run "${ARGS[@]}" src/main.ts
elif grep -q "DEBUG=2" .env; then
- deno test "${ARGS[@]}" test/ &&
+ deno lint --unstable &&
+ deno test "${ARGS[@]}" test/ &&
echo "Tests succeeded!"
else
deno run "${ARGS[@]}" src/main.ts
diff --git a/src/db/DBController.ts b/src/db/DBController.ts
index 1bb91ba..cb714b3 100644
--- a/src/db/DBController.ts
+++ b/src/db/DBController.ts
@@ -33,19 +33,17 @@ export default class DBController {
}
}
- async query(query: string, params?: (boolean | number | any)[]) {
+ async query(query: string, params?: (boolean | number | string)[]) {
if (!this.client) await this.connect();
try {
- const res = await this.client!.query(query, params);
- console.log(res);
- return res;
+ return await this.client!.query(query, params);
} catch (e) {
throw e;
}
}
- async execute(query: string, params?: (boolean | number | any)[]) {
+ async execute(query: string, params?: (boolean | number | string)[]) {
if (!this.client) await this.connect();
try {
@@ -55,7 +53,7 @@ export default class DBController {
}
}
- async execute_multiple(queries: ((boolean | number | any)[] | string)[][]) {
+ async execute_multiple(queries: ((boolean | number | string)[] | string)[][]) {
if (!this.client) await this.connect();
try {
diff --git a/src/db/tables.sql b/src/db/tables.sql
index af9bd05..a0c9eef 100644
--- a/src/db/tables.sql
+++ b/src/db/tables.sql
@@ -1,5 +1,5 @@
-DROP TABLE IF EXISTS access;
-DROP TABLE IF EXISTS users;
+# DROP TABLE IF EXISTS access;
+# DROP TABLE IF EXISTS users;
CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
diff --git a/src/db/user.ts b/src/db/user.ts
index 3041c78..c7bed17 100644
--- a/src/db/user.ts
+++ b/src/db/user.ts
@@ -14,7 +14,7 @@ class User {
* @param password
* @param isAdmin
*/
- async createUser(email: string, username: string, password: string, isAdmin: boolean = false): Promise<boolean> {
+ async createUser(email: string, username: string, password: string, isAdmin = false): Promise<boolean> {
const salt = await genSalt(12);
const passwordHash = await hash(password, salt);
const verification = this.generateId();
@@ -32,19 +32,24 @@ class User {
/**
* Checks if the user provided password is correct
* @param username
- * @param password
+ * @param plainTextPassword
*/
- async login(username: string, password: string) {
- const dbUser = (
+ async login(username: string, plainTextPassword: string): Promise<loginData> {
+ const { uid, password, verification, darkTheme } = (
await this.controller.query(
- "SELECT id, password, verification, dark_theme, is_admin FROM users WHERE username = ?",
+ "SELECT id as uid, password, verification, dark_theme as darkTheme FROM users WHERE username = ?",
[username]
)
)[0];
- if (compare(password, dbUser.password)) {
- return true;
+ if (compare(plainTextPassword, password)) {
+ return {
+ success: true,
+ uid,
+ darkTheme,
+ verification,
+ };
} else {
- return false;
+ return { success: false };
}
}
@@ -54,7 +59,7 @@ class User {
* @private
*/
// TODO: Improve
- private generateId(len: number = 64): string {
+ private generateId(len = 64): string {
const values = new Uint8Array(len / 2);
crypto.getRandomValues(values);
return Array.from(values, (dec) => ("0" + dec.toString(36)).substr(-2)).join("");
diff --git a/src/handler/user.ts b/src/handler/user.ts
index 9881439..e194008 100644
--- a/src/handler/user.ts
+++ b/src/handler/user.ts
@@ -1,17 +1,25 @@
import type { HandlerFunc, Context } from "https://deno.land/x/abc@master/mod.ts";
-import db from "../db/user.ts";
+import db, {loginData} from "../db/user.ts";
export const index: HandlerFunc = async (c: Context) => c.params.name;
-
export const register: HandlerFunc = async (c: Context) => {
const { username, email, password } = await c.body();
const success = await db.createUser(email, username, password);
// TODO: Send email
- return { success };
+ return {success};
};
-
export const login: HandlerFunc = async (c: Context) => {
const { username, password } = await c.body();
- const success = await db.login(username, password);
- return { success };
-};
+ const data: loginData = await db.login(username, password);
+ if (data.success) {
+ c.setCookie({
+ name: "uid",
+ value: data.uid!.toString(),
+ });
+ c.setCookie({
+ name: "verification",
+ value: data.verification!,
+ })
+ }
+ return {"success": data.success};
+}