Skip to content
Snippets Groups Projects
Commit 8cd4a5d8 authored by narindra.rajohnso's avatar narindra.rajohnso
Browse files

correct api response

parent bb7929e6
No related branches found
No related tags found
No related merge requests found
import {Sequelize} from 'sequelize'; import {Sequelize} from 'sequelize';
import * as path from "path"; import * as path from "path";
import {userType} from "./models/User"; import {User, userType} from "./models/User";
import {initUser} from "./migrations/User_init"; import {initUser} from "./migrations/User_init";
import {initQuestion} from "./migrations/Question_init"; import {initQuestion} from "./migrations/Question_init";
import {User} from "./models/User"
import {Question} from "./models/Question"; import {Question} from "./models/Question";
...@@ -90,14 +89,8 @@ export class Database{ ...@@ -90,14 +89,8 @@ export class Database{
} }
static async checkUserPassword(username: string, password: string): Promise<boolean>{ static async infoUser(username: string): Promise<User>{
const user= await User.findOne({ where: { username } }); return await User.findOne({where: {username}});
if(user){
return password==user.dataValues.password
}else{
return false
}
} }
......
No preview for this file type
...@@ -34,7 +34,7 @@ export async function checkExistingUser(req: express.Request, res: express.Respo ...@@ -34,7 +34,7 @@ export async function checkExistingUser(req: express.Request, res: express.Respo
console.log(`type = ${typeAccount}`); console.log(`type = ${typeAccount}`);
console.log({message: typeAccount === 'user' ? `"${id}" => user` : `"${id}" => admin`}); console.log({message: typeAccount === 'user' ? `"${id}" => user` : `"${id}" => admin`});
}else{ }else{
res.status(StatusCodes.NOT_FOUND).json({message: 'Username not exist'}); res.status(StatusCodes.NOT_FOUND).json({message: 'USER_NOT_FOUND'});
} }
next(); next();
...@@ -93,10 +93,10 @@ export function createAccountCheck(req: express.Request, res: express.Response){ ...@@ -93,10 +93,10 @@ export function createAccountCheck(req: express.Request, res: express.Response){
}}); }});
}else{ }else{
if(result[1] === "Exist"){ 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 => { }).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
...@@ -21,11 +21,18 @@ router.post('/create-account', checkUserFields, (req: express.Request, res: expr ...@@ -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) => { router.post('/:username', checkExistingUser, async (req: express.Request, res: express.Response) => {
const data = req.body const data = req.body
if (await Database.checkUserPassword(req.params.username, data.password)) { let user=await Database.infoUser(req.params.username);
res.status(StatusCodes.OK).json({message: "User can be authentified"}) 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{ }else{
res.status(StatusCodes.BAD_REQUEST).json({message: "User cannot be authentified"}) res.status(StatusCodes.BAD_REQUEST).json({message: "USER_PASSWORD_FALSE"})
} }
}
}); });
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment