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

correct api response

parent bb7929e6
Branches
No related tags found
No related merge requests found
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}});
}
......
No preview for this file type
......@@ -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
......@@ -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"})
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 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 register or to comment