This commit is contained in:
2024-09-27 17:20:37 +02:00
parent ee932a2339
commit f43e802501
59 changed files with 810 additions and 236 deletions
+21 -20
View File
@@ -3,6 +3,7 @@ const moment = require('moment-timezone');
const { ObjectId } = require('mongodb');
const { UserDb } = require("../services/userDb");
const crypto = require('crypto');
const { error } = require("console");
function ClearUserData(user){
delete user.salt;
@@ -33,7 +34,7 @@ exports.get = asyncHandler(async (req, res, next) => {
}
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -45,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("User already exists");
return res.status(500).send({error: "User already exists"});
}
// check password
if(!req.body.password){
return res.status(500).send("Password not set");
return res.status(500).send({error: "Password not set"});
}
if(req.body.password != req.body.confirmPassword){
return res.status(500).send("Passwords do not match");
return res.status(500).send({error: "Passwords do not match"});
}
if(req.body.password.length < 8){
return res.status(500).send("Password too short");
return res.status(500).send({error: "Password too short"});
}
if(req.body.isAdmin){
if(req.user){
if(!req.user.isAdmin){
return res.status(500).send("You are not allowed to create an admin user");
return res.status(500).send({error: "You are not allowed to create an admin user"});
}
}else{
req.body.isAdmin = false
@@ -72,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("You are not allowed to create an agent user");
return res.status(500).send({error: "You are not allowed to create an agent user"});
}
}else{
req.body.isAgent = false
@@ -91,10 +92,10 @@ exports.post = asyncHandler(async (req, res, next) => {
let createData = await userDb.post(user);
res.status(204).send();
res.status(204).send({message: "User created"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -107,7 +108,7 @@ exports.put = asyncHandler(async (req, res, next) => {
const User = await userDb.get(id);
if(!User){
return res.status(500).send("User not found");
return res.status(500).send({error:"User not found"});
}
// check password
@@ -115,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("Passwords do not match");
return res.status(500).send({error:"Passwords do not match"});
}
if(req.body.password.length < 8){
return res.status(500).send("Password too short");
return res.status(500).send({error:"Password too short"});
}
salt = crypto.randomBytes(16).toString('hex');
hashed_password = crypto.pbkdf2Sync(req.body.password, salt, 310000, 32, 'sha256').toString('hex');
@@ -129,12 +130,12 @@ exports.put = asyncHandler(async (req, res, next) => {
if(req.body.isAdmin){
if(!req.user.isAdmin){
return res.status(500).send("You are not allowed to create an admin user");
return res.status(500).send({error:"You are not allowed to create an admin user"});
}
}
if(req.body.isAgent){
if(!req.user.isAdmin){
return res.status(500).send("You are not allowed to create an agent user");
return res.status(500).send({error:"You are not allowed to create an agent user"});
}
}
@@ -152,7 +153,7 @@ exports.put = asyncHandler(async (req, res, next) => {
res.status(200).send(result);
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -168,7 +169,7 @@ exports.delete = asyncHandler(async (req, res, next) => {
res.status(200).send({"message": "User deleted"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -180,7 +181,7 @@ exports.current = asyncHandler(async (req, res, next) => {
res.status(200).send(user);
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -188,10 +189,10 @@ exports.current = asyncHandler(async (req, res, next) => {
exports.agentConnected = asyncHandler(async (req, res, next) => {
try{
res.status(200).send("OK");
res.status(200).send({message: "Agent connected"});
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});
@@ -203,7 +204,7 @@ exports.getAllUsers = asyncHandler(async (req, res, next) => {
res.status(200).send(result);
}catch(err){
console.log(err);
return res.status(500).send(err);
return res.status(500).send({error: err});
}
});