From 8cd4a5d8dcf63257747993ac012ce194df4b5a61 Mon Sep 17 00:00:00 2001 From: "narindra.rajohnso" <narindra-hasimanjaka-david.rajohnson@etu.hesge.ch> Date: Tue, 16 May 2023 18:57:41 +0200 Subject: [PATCH] correct api response --- API/src/database/Database.ts | 13 +++---------- API/src/database/database.sqlite | Bin 20480 -> 20480 bytes API/src/routes/middleware.ts | 6 +++--- API/src/routes/router-guest.ts | 15 +++++++++++---- 4 files changed, 17 insertions(+), 17 deletions(-) diff --git a/API/src/database/Database.ts b/API/src/database/Database.ts index aed639a..47bd801 100644 --- a/API/src/database/Database.ts +++ b/API/src/database/Database.ts @@ -1,9 +1,8 @@ import {Sequelize} from 'sequelize'; import * as path from "path"; -import {userType} from "./models/User"; +import {User, userType} from "./models/User"; import {initUser} from "./migrations/User_init"; import {initQuestion} from "./migrations/Question_init"; -import {User} from "./models/User" import {Question} from "./models/Question"; @@ -90,14 +89,8 @@ export class Database{ } - static async checkUserPassword(username: string, password: string): Promise<boolean>{ - const user= await User.findOne({ where: { username } }); - if(user){ - return password==user.dataValues.password - }else{ - return false - } - + static async infoUser(username: string): Promise<User>{ + return await User.findOne({where: {username}}); } diff --git a/API/src/database/database.sqlite b/API/src/database/database.sqlite index 5a843bb909e96719d7fe7976748a1bcf9692a046..6a3bf9c8400809ec4f207d5000d4cfa90723207f 100644 GIT binary patch delta 320 zcmZozz}T>Wae_3X{zMsPM*WQmOZd5%`Pv!ymH68E`!)*-RPxm~N-%S(r;17{i+g(J zCKl(H7MCPenpqf_milK_Rc59a>YJ9O`B=D=`s8`!l^0ir<>h&MIhq(pT9%Y&n`l>6 z<rR3^x+JER1SMwWXXK?Mrj<Bkq!#NXXC$WNX66|g7#ZstnCcpuDHxhr8JJobTI!h_ z87OEQ7+4t?7#YYQOB<P6nHcI>T9`nkCo9S4GO|pbAaCmk)^22OWoV&iX>J6SM>QN> zmW%%f12g|+2L8+Z@A&U*7F0OOugSqI$_R2Iiy*T)BZ_yKIhn;7Q;O4yQ<xYS7&tcn I(PvQr06w==!2kdN delta 132 zcmZozz}T>Wae_3X_Cy(HM(vFWOZYjM_>~y=`}mbM3o3~5Pgau8Wn`Q@LEhHhRKdvD z%Gl7#z*x`7&_Y4mz`)ADz{tSJSl7T**AS~L7Xt$W6aPB~{vZ7BHVZmD;^*UJ7H3Q; SPAg7fVrLfR-26wMMF9ZmZyr7X diff --git a/API/src/routes/middleware.ts b/API/src/routes/middleware.ts index 9ea44d2..0c49da6 100644 --- a/API/src/routes/middleware.ts +++ b/API/src/routes/middleware.ts @@ -34,7 +34,7 @@ export async function checkExistingUser(req: express.Request, res: express.Respo console.log(`type = ${typeAccount}`); console.log({message: typeAccount === 'user' ? `"${id}" => user` : `"${id}" => admin`}); }else{ - res.status(StatusCodes.NOT_FOUND).json({message: 'Username not exist'}); + res.status(StatusCodes.NOT_FOUND).json({message: 'USER_NOT_FOUND'}); } next(); @@ -93,10 +93,10 @@ export function createAccountCheck(req: express.Request, res: express.Response){ }}); }else{ if(result[1] === "Exist"){ - res.status(StatusCodes.NOT_ACCEPTABLE).json({error: "Username already exist"}); + res.status(StatusCodes.NOT_ACCEPTABLE).json({message: "USER_EXIST"}); } } }).catch(error => { - res.status(StatusCodes.BAD_REQUEST).json({error: "An error occured"}); + res.status(StatusCodes.BAD_REQUEST).json({message: "An error occured"}); }); } \ No newline at end of file diff --git a/API/src/routes/router-guest.ts b/API/src/routes/router-guest.ts index a23f51f..79500bc 100644 --- a/API/src/routes/router-guest.ts +++ b/API/src/routes/router-guest.ts @@ -21,11 +21,18 @@ router.post('/create-account', checkUserFields, (req: express.Request, res: expr router.post('/:username', checkExistingUser, async (req: express.Request, res: express.Response) => { const data = req.body - if (await Database.checkUserPassword(req.params.username, data.password)) { - res.status(StatusCodes.OK).json({message: "User can be authentified"}) - }else{ - res.status(StatusCodes.BAD_REQUEST).json({message: "User cannot be authentified"}) + let user=await Database.infoUser(req.params.username); + if(user){ + let usertype=user.dataValues.type === "user"?"USER":"ADMIN" + if (user.dataValues.password === data.password) { + res.status(StatusCodes.OK).json({message: usertype+"_ALLOWED"}) + }else{ + res.status(StatusCodes.BAD_REQUEST).json({message: "USER_PASSWORD_FALSE"}) + } } + + + }); -- GitLab