diff options
Diffstat (limited to 'src/db')
-rw-r--r-- | src/db/DBController.ts | 10 | ||||
-rw-r--r-- | src/db/tables.sql | 4 | ||||
-rw-r--r-- | src/db/user.ts | 23 |
3 files changed, 20 insertions, 17 deletions
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(""); |