Skip to content
Snippets Groups Projects
Commit 8dd3a614 authored by alec.schmidt's avatar alec.schmidt
Browse files

backend refactor done

parent 5b941608
Branches
No related tags found
No related merge requests found
No preview for this file type
...@@ -5,7 +5,7 @@ CREATE TABLE users ( ...@@ -5,7 +5,7 @@ CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT, id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE, username TEXT UNIQUE,
password TEXT, password TEXT,
type TEXT admin BOOLEAN
); );
CREATE TABLE questions ( CREATE TABLE questions (
...@@ -28,8 +28,8 @@ CREATE TABLE answer ( ...@@ -28,8 +28,8 @@ CREATE TABLE answer (
); );
INSERT INTO users (username, password, type) INSERT INTO users (username, password, admin)
VALUES ('alec', 'alec', 'admin'); VALUES ('alec', 'alec', true);
INSERT INTO category (title) INSERT INTO category (title)
VALUES ('Histoire'), ('Géographie'); VALUES ('Histoire'), ('Géographie');
......
...@@ -15,7 +15,7 @@ export type User_t = { ...@@ -15,7 +15,7 @@ export type User_t = {
id: number; id: number;
username: string; username: string;
password: string; password: string;
type: string; admin: boolean;
}; };
export type Question_t = { export type Question_t = {
...@@ -47,7 +47,7 @@ class DBHandler { ...@@ -47,7 +47,7 @@ class DBHandler {
else else
throw JSON.stringify({message: "Username does not exists"}); throw JSON.stringify({message: "Username does not exists"});
if (okay) if (okay)
return await asyncdb.get("SELECT username, type FROM users WHERE username='" + user.username + "'") return await asyncdb.get("SELECT username, admin FROM users WHERE username='" + user.username + "'")
else else
throw JSON.stringify({message: "Incorrect Password"}); throw JSON.stringify({message: "Incorrect Password"});
} }
...@@ -61,13 +61,13 @@ class DBHandler { ...@@ -61,13 +61,13 @@ class DBHandler {
} }
async comparePassword(user: User_t) : Promise<boolean> { async comparePassword(user: User_t) : Promise<boolean> {
const query = "SELECT password, type FROM users WHERE username='"+ user.username + "'"; const query = "SELECT password, admin FROM users WHERE username='"+ user.username + "'";
const password = await asyncdb.all<User_t>(query) const password = await asyncdb.all<User_t>(query)
return (password[0].password == user.password); return (password[0].password == user.password);
} }
async getUsers(res:express.Response) { async getUsers(res:express.Response) {
const query = "SELECT id, username, type FROM users"; const query = "SELECT id, username, admin FROM users";
asyncdb.all(query) asyncdb.all(query)
.then(result => { .then(result => {
res.status(StatusCodes.OK).json(result).end()} res.status(StatusCodes.OK).json(result).end()}
...@@ -78,11 +78,11 @@ class DBHandler { ...@@ -78,11 +78,11 @@ class DBHandler {
async postUser(req:express.Request, res:express.Response) { async postUser(req:express.Request, res:express.Response) {
let a = req.body as User_t; let a = req.body as User_t;
if (a.type === undefined) if (a.admin === undefined)
a.type = "player" a.admin = false;
const query = "INSERT INTO users (username, password, type) \ const query = "INSERT INTO users (username, password, admin) \
VALUES ('" + a.username + "','" + a.password + "','" + a.type +"')"; VALUES ('" + a.username + "','" + a.password + "','" + a.admin +"')";
asyncdb.all(query) asyncdb.all(query)
.then( () => res.status(StatusCodes.OK)) .then( () => res.status(StatusCodes.OK))
...@@ -93,7 +93,7 @@ class DBHandler { ...@@ -93,7 +93,7 @@ class DBHandler {
const a = req.body as User_t; const a = req.body as User_t;
const request = "UPDATE users \ const request = "UPDATE users \
SET username = '"+ a.username + "', type = '" + a.type + "' WHERE id = " + req.params.id; SET username = '"+ a.username + "', admin = '" + a.admin + "' WHERE id = " + req.params.id;
asyncdb.all(request) asyncdb.all(request)
.then(() => res.status(StatusCodes.OK).end()) .then(() => res.status(StatusCodes.OK).end())
......
...@@ -29,7 +29,7 @@ function isAdmin(token: string): Boolean { ...@@ -29,7 +29,7 @@ function isAdmin(token: string): Boolean {
// console.log(err); // console.log(err);
if (err) return false; if (err) return false;
adminToken = user.type === "admin" adminToken = user.admin
}); });
return adminToken; return adminToken;
...@@ -99,7 +99,7 @@ router.patch(ROUTE+'/user/:id', (req: express.Request, res: express.Response) => ...@@ -99,7 +99,7 @@ router.patch(ROUTE+'/user/:id', (req: express.Request, res: express.Response) =>
console.log(a); console.log(a);
if (a.type === "admin") if (a.admin === true)
if (!isAdmin(req.headers['authorization'] && req.headers['authorization'].split(' ')[1])) if (!isAdmin(req.headers['authorization'] && req.headers['authorization'].split(' ')[1]))
res.status(StatusCodes.UNAUTHORIZED).end(); res.status(StatusCodes.UNAUTHORIZED).end();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment