aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMarvin Borner2020-07-18 22:37:00 +0200
committerMarvin Borner2020-07-18 22:38:45 +0200
commitaccb147b54bc99e5cd20059be5cb333031f15885 (patch)
tree8523ad288cb4fbf84acd6a646bd7c04957298bf4 /src
parentd3c4b447c3d882a77282e32d9e12d7c3d4f034a2 (diff)
Added user registration
Co-authored-by: LarsVomMars <lars@kroenner.eu>
Diffstat (limited to 'src')
-rw-r--r--src/db/DBController.ts19
-rw-r--r--src/db/tables.sql2
-rw-r--r--src/db/user.ts64
-rw-r--r--src/groups/user.ts3
-rw-r--r--src/handler/user.ts12
5 files changed, 86 insertions, 14 deletions
diff --git a/src/db/DBController.ts b/src/db/DBController.ts
index 416f695..1bb91ba 100644
--- a/src/db/DBController.ts
+++ b/src/db/DBController.ts
@@ -11,7 +11,6 @@ export default class DBController {
const queries = sql.split(";");
queries.pop();
for (const query of queries) await this.execute(query);
- // queries.forEach(async (query) => await this.execute(query));
console.log("Tables created");
} catch (e) {
console.error("Could not create tables");
@@ -34,28 +33,30 @@ export default class DBController {
}
}
- async query(query: string, params?: string[]) {
- if (!this.client) throw Error("Database isn't initialized yet!");
+ async query(query: string, params?: (boolean | number | any)[]) {
+ if (!this.client) await this.connect();
try {
- return await this.client.query(query, params);
+ const res = await this.client!.query(query, params);
+ console.log(res);
+ return res;
} catch (e) {
throw e;
}
}
- async execute(query: string, params?: string[]) {
- if (!this.client) throw Error("Database isn't initialized yet!");
+ async execute(query: string, params?: (boolean | number | any)[]) {
+ if (!this.client) await this.connect();
try {
- return await this.client.execute(query, params);
+ return await this.client!.execute(query, params);
} catch (e) {
throw e;
}
}
- async execute_multiple(queries: (string[] | string)[][]) {
- if (!this.client) throw Error("Database isn't initialized yet!");
+ async execute_multiple(queries: ((boolean | number | any)[] | string)[][]) {
+ if (!this.client) await this.connect();
try {
await this.client!.transaction(async (conn) => {
diff --git a/src/db/tables.sql b/src/db/tables.sql
index 9883ea1..af9bd05 100644
--- a/src/db/tables.sql
+++ b/src/db/tables.sql
@@ -3,7 +3,7 @@ DROP TABLE IF EXISTS users;
CREATE TABLE IF NOT EXISTS users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
- email VARCHAR(24) NOT NULL UNIQUE,
+ email VARCHAR(48) NOT NULL UNIQUE,
username VARCHAR(24) NOT NULL UNIQUE,
password VARCHAR(64) NOT NULL,
verification VARCHAR(64) NOT NULL UNIQUE,
diff --git a/src/db/user.ts b/src/db/user.ts
index c0cb127..3041c78 100644
--- a/src/db/user.ts
+++ b/src/db/user.ts
@@ -1,9 +1,71 @@
import DBController from "./DBController.ts";
+import { hash, compare, genSalt } from "https://deno.land/x/bcrypt/mod.ts";
class User {
- createUser() {
+ private controller: DBController;
+ constructor() {
+ this.controller = new DBController();
+ }
+
+ /**
+ * Creates new user
+ * @param email
+ * @param username
+ * @param password
+ * @param isAdmin
+ */
+ async createUser(email: string, username: string, password: string, isAdmin: boolean = false): Promise<boolean> {
+ const salt = await genSalt(12);
+ const passwordHash = await hash(password, salt);
+ const verification = this.generateId();
+ try {
+ await this.controller.execute(
+ "INSERT INTO users (email, username, password, verification, is_admin) VALUE (?, ?, ?, ?, ?)",
+ [email, username, passwordHash, verification, isAdmin]
+ );
+ return true;
+ } catch (e) {
+ throw e;
+ }
+ }
+ /**
+ * Checks if the user provided password is correct
+ * @param username
+ * @param password
+ */
+ async login(username: string, password: string) {
+ const dbUser = (
+ await this.controller.query(
+ "SELECT id, password, verification, dark_theme, is_admin FROM users WHERE username = ?",
+ [username]
+ )
+ )[0];
+ if (compare(password, dbUser.password)) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * Generate random id
+ * @param len
+ * @private
+ */
+ // TODO: Improve
+ private generateId(len: number = 64): string {
+ const values = new Uint8Array(len / 2);
+ crypto.getRandomValues(values);
+ return Array.from(values, (dec) => ("0" + dec.toString(36)).substr(-2)).join("");
}
}
export default new User();
+
+export interface loginData {
+ success: boolean;
+ uid?: number;
+ verification?: string;
+ darkTheme?: string;
+}
diff --git a/src/groups/user.ts b/src/groups/user.ts
index 614e432..b8518dc 100644
--- a/src/groups/user.ts
+++ b/src/groups/user.ts
@@ -1,7 +1,8 @@
import type { Group, Context } from "https://deno.land/x/abc@master/mod.ts";
-// import type { Group, Context } from "../abc/mod.ts";
import * as handlers from "../handler/user.ts";
export default function (g: Group) {
g.get("/:name", handlers.index);
+ g.post("/register", handlers.register);
+ g.post("/login", handlers.login);
}
diff --git a/src/handler/user.ts b/src/handler/user.ts
index fe65eaa..9881439 100644
--- a/src/handler/user.ts
+++ b/src/handler/user.ts
@@ -2,8 +2,16 @@ import type { HandlerFunc, Context } from "https://deno.land/x/abc@master/mod.ts
import db 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();
- await db.createUser(email, username, password);
+ const success = await db.createUser(email, username, password);
+ // TODO: Send email
+ return { success };
+};
-}
+export const login: HandlerFunc = async (c: Context) => {
+ const { username, password } = await c.body();
+ const success = await db.login(username, password);
+ return { success };
+};