diff --git a/API/db/app.db b/API/db/app.db index 56182c95bd846eb45693d59fe952a2c56c3ec843..c1b39459ffb292715ef4a0060a6bbfe525c322b4 100644 Binary files a/API/db/app.db and b/API/db/app.db differ diff --git a/API/db/create_db.sql b/API/db/create_db.sql index 2bdd0de7ce13aa500093f4536cefdeaf474615d8..ceb75d81daa8edda83a5b5157a72f2c880c3f73c 100644 --- a/API/db/create_db.sql +++ b/API/db/create_db.sql @@ -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'); diff --git a/API/src/database/Database.ts b/API/src/database/Database.ts index 5a94f6f558107a7ebaf69204dd23555c5c5fd056..d5f1a102918a25dd8dac56c7f678768e7541b7ed 100644 --- a/API/src/database/Database.ts +++ b/API/src/database/Database.ts @@ -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()) diff --git a/API/src/routes/BaseRoutes.ts b/API/src/routes/BaseRoutes.ts index 5c9db808c55e4b7369a19ec5b337009139348a9d..03827838ebba38a23246000f2586475cac8baf11 100644 --- a/API/src/routes/BaseRoutes.ts +++ b/API/src/routes/BaseRoutes.ts @@ -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();