From 08565c9edab879b00e8da473b170dc782454f24d Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 12 Jun 2024 13:30:56 -0700 Subject: [PATCH 1/3] Simplified form control in Login and Register. Fixed registration bug. Throw better errors --- Client/src/Pages/Login/index.jsx | 11 +---------- Client/src/Pages/Register/index.jsx | 11 +---------- Server/controllers/alertController.js | 2 -- Server/controllers/authController.js | 16 ++-------------- Server/db/MongoDB.js | 4 ++++ Server/middleware/handleErrors.js | 1 - 6 files changed, 8 insertions(+), 37 deletions(-) diff --git a/Client/src/Pages/Login/index.jsx b/Client/src/Pages/Login/index.jsx index 01cce9ded..8bee2d1d4 100644 --- a/Client/src/Pages/Login/index.jsx +++ b/Client/src/Pages/Login/index.jsx @@ -44,7 +44,7 @@ const Login = () => { } else { setErrors({}); } - }, []); + }, [form]); const handleSubmit = async () => { try { @@ -77,15 +77,6 @@ const Login = () => { }; const handleInput = (e) => { - const fieldName = idMap[e.target.id]; - // Extract and validate individual fields as input changes - const fieldSchema = loginValidation.extract(fieldName); - const { error } = fieldSchema.validate(e.target.value); - let errMsg = ""; - if (error) { - errMsg = error.message; - } - setErrors({ ...errors, [fieldName]: errMsg }); const newForm = { ...form, [idMap[e.target.id]]: e.target.value }; setForm(newForm); }; diff --git a/Client/src/Pages/Register/index.jsx b/Client/src/Pages/Register/index.jsx index 81d458873..70181e194 100644 --- a/Client/src/Pages/Register/index.jsx +++ b/Client/src/Pages/Register/index.jsx @@ -52,18 +52,9 @@ const Register = () => { } else { setErrors({}); } - }, []); + }, [form]); const handleInput = (e) => { - const fieldName = idMap[e.target.id]; - // Extract and validate individual fields as input changes - const fieldSchema = registerValidation.extract(fieldName); - const { error } = fieldSchema.validate(e.target.value); - let errMsg = ""; - if (error) { - errMsg = error.message; - } - setErrors({ ...errors, [fieldName]: errMsg }); const newForm = { ...form, [idMap[e.target.id]]: e.target.value }; setForm(newForm); }; diff --git a/Server/controllers/alertController.js b/Server/controllers/alertController.js index f0c3df5dd..bcc408a25 100644 --- a/Server/controllers/alertController.js +++ b/Server/controllers/alertController.js @@ -1,4 +1,3 @@ -const express = require("express"); const logger = require("../utils/logger"); require("dotenv").config(); const { @@ -11,7 +10,6 @@ const { editAlertBodyValidation, deleteAlertParamValidation, } = require("../validation/joi"); -const { get } = require("mongoose"); const SERVICE_NAME = "alerts"; diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 0391a904f..7730219ea 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -45,18 +45,6 @@ const registerController = async (req, res, next) => { return; } - // Check if the user exists - try { - const isUser = await req.db.getUserByEmail(req, res); - if (isUser) { - throw new Error("User already exists"); - } - } catch (error) { - error.service = SERVICE_NAME; - next(error); - return; - } - // Create a new user try { const newUser = await req.db.insertUser(req, res); @@ -101,8 +89,8 @@ const loginController = async (req, res, next) => { // Compare password const match = await user.comparePassword(req.body.password); - if (!match) { - throw new Error("Password does not match!"); + if (match !== true) { + throw new Error("Incorrect password"); } // Remove password from user object. Should this be abstracted to DB layer? diff --git a/Server/db/MongoDB.js b/Server/db/MongoDB.js index 07804589e..4ec6e8422 100644 --- a/Server/db/MongoDB.js +++ b/Server/db/MongoDB.js @@ -5,6 +5,7 @@ const Check = require("../models/Check"); const Alert = require("../models/Alert"); const RecoveryToken = require("../models/RecoveryToken"); const crypto = require("crypto"); +const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key const connect = async () => { try { @@ -34,6 +35,9 @@ const insertUser = async (req, res) => { await newUser.save(); return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find } catch (error) { + if (error.code === DUPLICATE_KEY_CODE) { + throw new Error("Email already exists"); + } throw error; } }; diff --git a/Server/middleware/handleErrors.js b/Server/middleware/handleErrors.js index a20db8571..b8690b438 100644 --- a/Server/middleware/handleErrors.js +++ b/Server/middleware/handleErrors.js @@ -4,7 +4,6 @@ const handleErrors = (error, req, res, next) => { const status = error.status || 500; const message = error.message || "Something went wrong"; const service = error.service || "Unknown service"; - logger.error(error.message, { service: service }); res.status(status).json({ success: false, msg: message }); }; From 3cc01db55f4db063c53a3900dece207d237ab33d Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Wed, 12 Jun 2024 13:49:38 -0700 Subject: [PATCH 2/3] Wrapped login and register in forms --- Client/src/Components/Button/index.jsx | 7 +- .../TextFields/Email/EmailTextField.jsx | 2 + .../TextFields/Password/PasswordTextField.jsx | 2 + .../Components/TextFields/Text/TextField.jsx | 2 + Client/src/Pages/Login/index.jsx | 11 +- Client/src/Pages/Register/index.jsx | 149 +++++++++--------- 6 files changed, 92 insertions(+), 81 deletions(-) diff --git a/Client/src/Components/Button/index.jsx b/Client/src/Components/Button/index.jsx index 6f67f0c02..72c438514 100644 --- a/Client/src/Components/Button/index.jsx +++ b/Client/src/Components/Button/index.jsx @@ -36,6 +36,7 @@ const levelConfig = { * @component * @param {Object} props * @param {'primary' | 'secondary' | 'tertiary' | 'error' | 'imagePrimary' | 'imageSecondary' | 'imageTertiary'} props.level - The level of the button + * @param {string} props.type - The type of the button * @param {string} props.label - The label of the button * @param {React.ReactNode} props.img - Image for button * @param {boolean} [props.disabled] - Whether the button is disabled @@ -44,13 +45,14 @@ const levelConfig = { * @returns {JSX.Element} * @example * // Render an error button - *