From 9dd4d10396cc95f732b97dd1d8cd1256aa036442 Mon Sep 17 00:00:00 2001 From: Jonasz Bigda Date: Mon, 8 Feb 2021 15:48:33 +0100 Subject: [PATCH] fixes --- models/users.js | 5 ++++- routes/routeUser.js | 11 ++++++++++- services/dataPrepServices.js | 38 ++++++++++++++++++++++++------------ services/services.js | 2 +- services/validations.js | 2 +- 5 files changed, 42 insertions(+), 16 deletions(-) diff --git a/models/users.js b/models/users.js index 86455f6..1d7f35b 100644 --- a/models/users.js +++ b/models/users.js @@ -36,7 +36,10 @@ const userSchema = mongoose.Schema({ maxlength: 64, }, }, - isRestaurant: Boolean, + isRestaurant: { + type: Boolean, + required: true + }, restaurants: [mongoose.Types.ObjectId], trialUsed: Boolean, preferences: { diff --git a/routes/routeUser.js b/routes/routeUser.js index 9f3db7b..c71be08 100644 --- a/routes/routeUser.js +++ b/routes/routeUser.js @@ -28,7 +28,7 @@ var router = express.Router(); router.post("/login", async (req, res) => { try { if (!req.body.password || !req.body.email) { - throw newError("Niepełne dane.", 204); + throw newError("Niepełne dane.", 403); } validateLogin(req.body); const user = await fetchUser(req.body.email); @@ -87,6 +87,15 @@ router.post("/register", async (req, res) => { } }); +// CHANGE USER DATA +router.post("/edit", async (req, res) => { + try { + console.log("23") + } catch (error) { + handleError(error, res) + } +}) + // CHANGE PASSWORD router.post("/changepass", async (req, res) => { try { diff --git a/services/dataPrepServices.js b/services/dataPrepServices.js index b552511..767111c 100644 --- a/services/dataPrepServices.js +++ b/services/dataPrepServices.js @@ -8,18 +8,30 @@ const { deleteImage } = require("./oceanServices.js"); async function createUser(request) { const password = await hashPass(request.body.password); - const user = new User({ - _id: new mongoose.Types.ObjectId(), - email: request.body.email, - password: password, - firstname: request.body.firstname, - lastname: request.body.lastname, - billing: { - NIP: request.body.NIP, - adress: request.body.adress, - companyName: request.body.companyName, - }, - }); + let user; + if(request.body.isRestaurant === true){ + user = new User({ + _id: new mongoose.Types.ObjectId(), + email: request.body.email, + password: password, + firstname: request.body.firstname, + lastname: request.body.lastname, + isRestaurant: true, + billing: { + NIP: request.body.NIP, + adress: request.body.adress, + companyName: request.body.companyName, + }, + }); + } else { + user = new User({ + _id: new mongoose.Types.ObjectId(), + email: request.body.email, + login: request.body.login, + password: password, + isRestaurant: false, + }); + } return user; } @@ -113,6 +125,8 @@ async function prepareSafeUser(user) { firstname: user.firstname, lastname: user.lastname, email: user.email, + login: user.login, + isRestaurant: user.isRestaurant, id: user._id, restaurants: restaurants, NIP: user.billing.NIP, diff --git a/services/services.js b/services/services.js index ccb4cd9..54d5ec3 100644 --- a/services/services.js +++ b/services/services.js @@ -40,7 +40,7 @@ function decodeAndSanitize(query) { async function checkPassword(password, hash) { const result = await bcrypt.compare(password, hash); - if (!result) throw newError("Hasło nieprawidłowe", 401); + if (!result) throw newError("Hasło nieprawidłowe", 403); } function generateAuthToken(user) { diff --git a/services/validations.js b/services/validations.js index ef65d76..da23d67 100644 --- a/services/validations.js +++ b/services/validations.js @@ -5,7 +5,7 @@ const validateLogin = function(requestBody){ const email = validator.isEmail(requestBody.email) && validator.isLength(requestBody.email, { max: 64 }) const password = validator.isLength(requestBody.password, { max: 64 }); if(!email || !password){ - throw newError("Dane logowania nieprawidłowe :/", 400); + throw newError("Dane logowania nieprawidłowe :/", 403); } }