update bug json
This commit is contained in:
+26
-26
@@ -28,13 +28,13 @@ exports.get = asyncHandler(async (req, res, next) => {
|
||||
const id = req.params.id;
|
||||
let result = await userDb.get(id);
|
||||
if (req.user.isAdmin){
|
||||
res.status(200).send(ClearUserDataForAdmin(result));
|
||||
res.status(200).json(ClearUserDataForAdmin(result));
|
||||
}else{
|
||||
res.status(200).send(ClearUserData(result));
|
||||
res.status(200).json(ClearUserData(result));
|
||||
}
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send({error: err});
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -46,24 +46,24 @@ exports.post = asyncHandler(async (req, res, next) => {
|
||||
// check if double
|
||||
let User = await userDb.getByEmail(req.body.email);
|
||||
if(User){
|
||||
return res.status(500).send({error: "User already exists"});
|
||||
return res.status(500).json({error: "User already exists"});
|
||||
}
|
||||
|
||||
// check password
|
||||
if(!req.body.password){
|
||||
return res.status(500).send({error: "Password not set"});
|
||||
return res.status(500).json({error: "Password not set"});
|
||||
}
|
||||
if(req.body.password != req.body.confirmPassword){
|
||||
return res.status(500).send({error: "Passwords do not match"});
|
||||
return res.status(500).json({error: "Passwords do not match"});
|
||||
}
|
||||
if(req.body.password.length < 8){
|
||||
return res.status(500).send({error: "Password too short"});
|
||||
return res.status(500).json({error: "Password too short"});
|
||||
}
|
||||
|
||||
if(req.body.isAdmin){
|
||||
if(req.user){
|
||||
if(!req.user.isAdmin){
|
||||
return res.status(500).send({error: "You are not allowed to create an admin user"});
|
||||
return res.status(500).json({error: "You are not allowed to create an admin user"});
|
||||
}
|
||||
}else{
|
||||
req.body.isAdmin = false
|
||||
@@ -73,7 +73,7 @@ exports.post = asyncHandler(async (req, res, next) => {
|
||||
if(req.body.isAgent){
|
||||
if(req.user){
|
||||
if(!req.user.isAgent){
|
||||
return res.status(500).send({error: "You are not allowed to create an agent user"});
|
||||
return res.status(500).json({error: "You are not allowed to create an agent user"});
|
||||
}
|
||||
}else{
|
||||
req.body.isAgent = false
|
||||
@@ -92,10 +92,10 @@ exports.post = asyncHandler(async (req, res, next) => {
|
||||
|
||||
let createData = await userDb.post(user);
|
||||
|
||||
res.status(204).send({message: "User created"});
|
||||
res.status(204).json({message: "User created"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send({error: err});
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -108,7 +108,7 @@ exports.put = asyncHandler(async (req, res, next) => {
|
||||
|
||||
const User = await userDb.get(id);
|
||||
if(!User){
|
||||
return res.status(500).send({error:"User not found"});
|
||||
return res.status(500).json({error:"User not found"});
|
||||
}
|
||||
|
||||
// check password
|
||||
@@ -116,10 +116,10 @@ exports.put = asyncHandler(async (req, res, next) => {
|
||||
let salt = "";
|
||||
if(req.body.password){
|
||||
if(req.body.password != req.body.confirmPassword){
|
||||
return res.status(500).send({error:"Passwords do not match"});
|
||||
return res.status(500).json({error:"Passwords do not match"});
|
||||
}
|
||||
if(req.body.password.length < 8){
|
||||
return res.status(500).send({error:"Password too short"});
|
||||
return res.status(500).json({error:"Password too short"});
|
||||
}
|
||||
salt = crypto.randomBytes(16).toString('hex');
|
||||
hashed_password = crypto.pbkdf2Sync(req.body.password, salt, 310000, 32, 'sha256').toString('hex');
|
||||
@@ -130,12 +130,12 @@ exports.put = asyncHandler(async (req, res, next) => {
|
||||
|
||||
if(req.body.isAdmin){
|
||||
if(!req.user.isAdmin){
|
||||
return res.status(500).send({error:"You are not allowed to create an admin user"});
|
||||
return res.status(500).json({error:"You are not allowed to create an admin user"});
|
||||
}
|
||||
}
|
||||
if(req.body.isAgent){
|
||||
if(!req.user.isAdmin){
|
||||
return res.status(500).send({error:"You are not allowed to create an agent user"});
|
||||
return res.status(500).json({error:"You are not allowed to create an agent user"});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -150,10 +150,10 @@ exports.put = asyncHandler(async (req, res, next) => {
|
||||
|
||||
let result = await userDb.put(id, user);
|
||||
console.log(result);
|
||||
res.status(200).send(result);
|
||||
res.status(200).json(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send({error: err});
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -166,10 +166,10 @@ exports.delete = asyncHandler(async (req, res, next) => {
|
||||
// Remove the sale
|
||||
await userDb.remove(id);
|
||||
|
||||
res.status(200).send({"message": "User deleted"});
|
||||
res.status(200).json({"message": "User deleted"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send({error: err});
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -178,10 +178,10 @@ exports.delete = asyncHandler(async (req, res, next) => {
|
||||
exports.current = asyncHandler(async (req, res, next) => {
|
||||
try{
|
||||
const user = ClearUserData(req.user);
|
||||
res.status(200).send(user);
|
||||
res.status(200).json(user);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send({error: err});
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
@@ -189,10 +189,10 @@ exports.current = asyncHandler(async (req, res, next) => {
|
||||
|
||||
exports.agentConnected = asyncHandler(async (req, res, next) => {
|
||||
try{
|
||||
res.status(200).send({message: "Agent connected"});
|
||||
res.status(200).json({message: "Agent connected"});
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send({error: err});
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
});
|
||||
|
||||
@@ -201,10 +201,10 @@ exports.getAllUsers = asyncHandler(async (req, res, next) => {
|
||||
const userDb = await UserDb.init();
|
||||
let result = await userDb.getAll();
|
||||
result = result.map(user => ClearUserDataForAdmin(user));
|
||||
res.status(200).send(result);
|
||||
res.status(200).json(result);
|
||||
}catch(err){
|
||||
console.log(err);
|
||||
return res.status(500).send({error: err});
|
||||
return res.status(500).json({error: err});
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user