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

backend refactor done

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