From ca33fcae9193188020f286a7ae2782cd5a2b5c38 Mon Sep 17 00:00:00 2001 From: veyselboybay Date: Tue, 14 May 2024 23:15:29 -0400 Subject: [PATCH] update on joi schema, added logger docs, and updated user model --- Server/controllers/authController.js | 17 ++++++++++++----- Server/controllers/monitorController.js | 5 +++++ Server/db/MongoDB.js | 4 ++-- Server/models/user.js | 7 ++++++- Server/validation/joi.js | 12 +++++++++--- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 23d5b8877..eba12db7d 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -1,6 +1,7 @@ const express = require("express"); const UserModel = require("../models/user"); -const { authValidation } = require("../validation/joi"); +const { registerValidation } = require("../validation/joi"); +const logger = require('../utils/logger') /** * @function @@ -11,7 +12,7 @@ const { authValidation } = require("../validation/joi"); const registerController = async (req, res) => { // joi validation try { - await authValidation.validateAsync(req.body); + await registerValidation.validateAsync(req.body); } catch (error) { return res .status(400) @@ -21,21 +22,27 @@ const registerController = async (req, res) => { // Check if the user exists try { const isUser = await req.db.getUserByEmail(req, res); - if (isUser) + if (isUser) { + logger.warning("User already exists!", { "service": "auth", "userId": isUser._id }); return res .status(400) .json({ success: false, msg: "User already exists!" }); + } } catch (error) { + logger.error(error.message, { "service": "auth" }); return res.status(500).json({ success: false, msg: error.message }); } + try { // Create a new user const newUser = await req.db.insertUser(req, res); - return res.json({ success: true, msg: "User created}", data: newUser }); - // Send an email to user + // TODO: Send an email to user // Will add this later + logger.info("New user created!", { "service": "auth", "userId": newUser._id }); + return res.json({ success: true, msg: "User created}", data: newUser }); } catch (error) { + logger.error(error.message, { "service": "auth" }); return res.status(500).json({ success: false, msg: error.message }); } }; diff --git a/Server/controllers/monitorController.js b/Server/controllers/monitorController.js index dda18d8ff..c85da0f9d 100644 --- a/Server/controllers/monitorController.js +++ b/Server/controllers/monitorController.js @@ -2,6 +2,7 @@ const { getMonitorsByIdValidation, getMonitorsByUserIdValidation, } = require("../validation/joi"); +const logger = require('../utils/logger') // Gets all monitors const getAllMonitors = async (req, res) => { @@ -9,6 +10,7 @@ const getAllMonitors = async (req, res) => { const monitors = await req.db.getAllMonitors(); return res.json({ success: true, msg: "Monitors found", data: monitors }); } catch (error) { + logger.error(error.message, { "service": "monitor" }); return res.status(500).json({ success: false, msg: error.message }); } }; @@ -27,6 +29,7 @@ const getMonitorById = async (req, res) => { const monitor = await req.db.getMonitorById(monitorId); return res.json({ success: true, msg: "Monitor found", data: monitor }); } catch (error) { + logger.error(error.message, { "service": "monitor" }); return res.status(500).json({ success: false, msg: error.message }); } }; @@ -43,12 +46,14 @@ const getMonitorsByUserId = async (req, res) => { try { const userId = req.params.userId; const monitors = await req.db.getMonitorsByUserId(userId); + logger.info(`Monitors for user ${userId} found`, { "service": "monitor", "userId":userId }); return res.json({ success: true, msg: `Monitors for user ${userId} found`, data: monitors, }); } catch (error) { + logger.error(error.message, { "service": "monitor" }); return res.status(500).json({ success: false, msg: error.message }); } }; diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 72f2d1d13..e3edbb7e2 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -14,7 +14,7 @@ const connect = async () => { const insertUser = async (req, res) => { try { - const newUser = await UserModel.create({ ...req.body }); + const newUser = await UserModel.create({ ...req.body }).select('-password'); return newUser; } catch (error) { throw error; @@ -24,7 +24,7 @@ const insertUser = async (req, res) => { const getUserByEmail = async (req, res) => { try { // Returns null if no user is found - const user = await UserModel.findOne({ email: req.body.email }); + const user = await UserModel.findOne({ email: req.body.email }).select('-password'); return user; } catch (error) { throw error; diff --git a/Server/models/user.js b/Server/models/user.js index 547192293..822c303d8 100644 --- a/Server/models/user.js +++ b/Server/models/user.js @@ -2,8 +2,13 @@ const mongoose = require("mongoose"); const bcrypt = require("bcrypt"); const UserSchema = mongoose.Schema({ - username: { + firstname: { type: String, + required: true, + }, + lastname: { + type: String, + required: true, }, email: { type: String, diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 53427527e..874daff93 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -1,8 +1,13 @@ const joi = require("joi"); const user = require("../models/user"); -const authValidation = joi.object({ - username: joi.string(), +const loginValidation = joi.object({ + email: joi.string().email().required(), + password: joi.string().min(8).required(), +}); +const registerValidation = joi.object({ + firstname: joi.string().required(), + lastname: joi.string().required(), email: joi.string().email().required(), password: joi.string().min(8).required(), }); @@ -16,7 +21,8 @@ const getMonitorsByUserIdValidation = joi.object({ }); module.exports = { - authValidation, + loginValidation, + registerValidation, getMonitorsByIdValidation, getMonitorsByUserIdValidation, };