Merge branch 'master' into feat/server-error-msgs

This commit is contained in:
Alex Holliday committed 2024-06-12 20:03:30 -07:00
commit 24aff84e2d
10 files changed
+102 -118

No files matched your search

+5 -2
View File
@@ -36,6 +36,7 @@ const levelConfig = {
* @component * @component
* @param {Object} props * @param {Object} props
* @param {'primary' | 'secondary' | 'tertiary' | 'error' | 'imagePrimary' | 'imageSecondary' | 'imageTertiary'} props.level - The level of the button * @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 {string} props.label - The label of the button
* @param {React.ReactNode} props.img - Image for button * @param {React.ReactNode} props.img - Image for button
* @param {boolean} [props.disabled] - Whether the button is disabled * @param {boolean} [props.disabled] - Whether the button is disabled
@@ -44,13 +45,14 @@ const levelConfig = {
* @returns {JSX.Element} * @returns {JSX.Element}
* @example * @example
* // Render an error button * // Render an error button
* <Button level="error" label="Error" disabled sx={{marginTop: "1rem"}}/> * <Button type="submit" level="error" label="Error" disabled sx={{marginTop: "1rem"}}/>
*/ */
const Button = ({ level, label, disabled, img, onClick, sx }) => { const Button = ({ type, level, label, disabled, img, onClick, sx }) => {
const { variant, color } = levelConfig[level]; const { variant, color } = levelConfig[level];
return ( return (
<MuiButton <MuiButton
type={type}
variant={variant} variant={variant}
color={color} color={color}
disabled={disabled} disabled={disabled}
@@ -67,6 +69,7 @@ const Button = ({ level, label, disabled, img, onClick, sx }) => {
}; };
Button.propTypes = { Button.propTypes = {
type: PropTypes.string,
level: PropTypes.string.isRequired, level: PropTypes.string.isRequired,
label: PropTypes.string.isRequired, label: PropTypes.string.isRequired,
img: PropTypes.node, img: PropTypes.node,
@@ -32,6 +32,7 @@ const EmailTextField = ({
label = "Email", label = "Email",
variant, variant,
placeholder, placeholder,
autoComplete,
icon, icon,
helperText, helperText,
error, error,
@@ -60,6 +61,7 @@ const EmailTextField = ({
id={id} id={id}
variant={variant} variant={variant}
placeholder={placeholder} placeholder={placeholder}
autoComplete={autoComplete}
InputProps={{ InputProps={{
endAdornment: ( endAdornment: (
<InputAdornment position="end"> <InputAdornment position="end">
@@ -22,6 +22,7 @@ const PasswordTextField = ({
onChange, onChange,
id, id,
label = "Password", label = "Password",
autoComplete,
variant, variant,
placeholder, placeholder,
icon, icon,
@@ -48,6 +49,7 @@ const PasswordTextField = ({
<TextField <TextField
onChange={onChange} onChange={onChange}
type="password" type="password"
autoComplete={autoComplete}
error={error} error={error}
className="password-text-field-input" className="password-text-field-input"
id={id} id={id}
@@ -19,6 +19,7 @@ import { useTheme } from "@mui/material";
*/ */
const StringTextField = ({ const StringTextField = ({
onChange, onChange,
autoComplete,
id, id,
label, label,
variant, variant,
@@ -51,6 +52,7 @@ const StringTextField = ({
id={id} id={id}
variant={variant} variant={variant}
placeholder={placeholder} placeholder={placeholder}
autoComplete={autoComplete}
InputProps={{ InputProps={{
endAdornment: ( endAdornment: (
<InputAdornment position="end"> <InputAdornment position="end">
+8 -14
View File
@@ -44,9 +44,10 @@ const Login = () => {
} else { } else {
setErrors({}); setErrors({});
} }
}, []); }, [form]);
const handleSubmit = async () => { const handleSubmit = async (e) => {
e.preventDefault();
try { try {
await loginValidation.validateAsync(form, { abortEarly: false }); await loginValidation.validateAsync(form, { abortEarly: false });
const action = await dispatch(login(form)); const action = await dispatch(login(form));
@@ -77,15 +78,6 @@ const Login = () => {
}; };
const handleInput = (e) => { 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 }; const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
setForm(newForm); setForm(newForm);
}; };
@@ -93,7 +85,7 @@ const Login = () => {
return ( return (
<div className="login-page"> <div className="login-page">
<BackgroundPattern></BackgroundPattern> <BackgroundPattern></BackgroundPattern>
<div className="login-form"> <form className="login-form" onSubmit={handleSubmit}>
<div className="login-form-header"> <div className="login-form-header">
<img <img
className="login-form-header-logo" className="login-form-header-logo"
@@ -110,6 +102,7 @@ const Login = () => {
error={errors.email ? true : false} error={errors.email ? true : false}
helperText={errors.email ? errors.email : ""} helperText={errors.email ? errors.email : ""}
placeholder="Enter your email" placeholder="Enter your email"
autoComplete="email"
id="login-email-input" id="login-email-input"
/> />
<div className="login-form-v2-spacing" /> <div className="login-form-v2-spacing" />
@@ -118,6 +111,7 @@ const Login = () => {
error={errors.password ? true : false} error={errors.password ? true : false}
helperText={errors.password ? errors.password : ""} helperText={errors.password ? errors.password : ""}
placeholder="Password" placeholder="Password"
autoComplete="current-password"
id="login-password-input" id="login-password-input"
/> />
</div> </div>
@@ -129,10 +123,10 @@ const Login = () => {
<div className="login-form-v3-spacing" /> <div className="login-form-v3-spacing" />
<div className="login-form-actions"> <div className="login-form-actions">
<Button <Button
type="submit"
level="primary" level="primary"
label="Sign in" label="Sign in"
sx={{ width: "100%" }} sx={{ width: "100%" }}
onClick={handleSubmit}
/> />
<div className="login-form-v-spacing" /> <div className="login-form-v-spacing" />
<Button <Button
@@ -147,7 +141,7 @@ const Login = () => {
Don’t have an account? Don’t have an account?
<span className="new-account-option-span">Sign up</span> <span className="new-account-option-span">Sign up</span>
</div> </div>
</div> </form>
</div> </div>
); );
}; };
+77 -85
View File
@@ -52,23 +52,15 @@ const Register = () => {
} else { } else {
setErrors({}); setErrors({});
} }
}, []); }, [form]);
const handleInput = (e) => { 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 }; const newForm = { ...form, [idMap[e.target.id]]: e.target.value };
setForm(newForm); setForm(newForm);
}; };
const handleSubmit = async () => { const handleSubmit = async (e) => {
e.preventDefault();
try { try {
await registerValidation.validateAsync(form, { abortEarly: false }); await registerValidation.validateAsync(form, { abortEarly: false });
const action = await dispatch(register(form)); const action = await dispatch(register(form));
@@ -103,82 +95,82 @@ const Register = () => {
return ( return (
<div className="register-page"> <div className="register-page">
<BackgroundPattern></BackgroundPattern> <BackgroundPattern></BackgroundPattern>
<form> <form className="register-form" onSubmit={handleSubmit}>
<div className="register-form"> <div className="register-form-header">
<div className="register-form-header"> <img
<img className="register-form-header-logo"
className="register-form-header-logo" src={Logomark}
src={Logomark} alt="Logomark"
alt="Logomark" />
/> <div className="register-form-v-spacing-large" />
<div className="register-form-v-spacing-large" /> <div className="register-form-heading">Create an account</div>
<div className="register-form-heading">Create an account</div> <div className="register-form-v-spacing-large"></div>
<div className="register-form-v-spacing-large"></div> </div>
</div> <div className="register-form-v-spacing-40px" />
<div className="register-form-v-spacing-40px" /> <div className="register-form-inputs">
<div className="register-form-inputs"> <StringTextField
<StringTextField onChange={handleInput}
onChange={handleInput} error={errors.firstname ? true : false}
error={errors.firstname ? true : false} helperText={errors.firstname ? errors.firstname : ""}
helperText={errors.firstname ? errors.firstname : ""} label="First name*"
label="First name*" placeholder="Enter your first name"
placeholder="Enter your first name" id="register-firstname-input"
id="register-firstname-input" />
/> <div className="login-form-v2-spacing" />
<div className="login-form-v2-spacing" /> <StringTextField
<StringTextField onChange={handleInput}
onChange={handleInput} error={errors.lastname ? true : false}
error={errors.lastname ? true : false} helperText={errors.lastname ? errors.lastname : ""}
helperText={errors.lastname ? errors.lastname : ""} label="Last name*"
label="Last name*" placeholder="Enter your last name"
placeholder="Enter your last name" id="register-lastname-input"
id="register-lastname-input" />
/> <div className="login-form-v2-spacing" />
<div className="login-form-v2-spacing" /> <EmailTextField
<EmailTextField onChange={handleInput}
onChange={handleInput} label="Email*"
label="Email*" error={errors.email ? true : false}
error={errors.email ? true : false} helperText={errors.email ? errors.email : ""}
helperText={errors.email ? errors.email : ""} placeholder="Enter your email"
placeholder="Enter your email" autoComplete="email"
id="register-email-input" id="register-email-input"
/> />
<div className="login-form-v2-spacing" /> <div className="login-form-v2-spacing" />
<PasswordTextField <PasswordTextField
onChange={handleInput} onChange={handleInput}
label="Password*" label="Password*"
error={errors.password ? true : false} error={errors.password ? true : false}
helperText={errors.password ? errors.password : ""} helperText={errors.password ? errors.password : ""}
placeholder="Create a password" placeholder="Create a password"
id="register-password-input" autoComplete="current-password"
/> id="register-password-input"
</div> />
<div className="login-form-v2-spacing" /> </div>
<div className="register-form-checks"> <div className="login-form-v2-spacing" />
<Check text="Must be at least 8 characters" /> <div className="register-form-checks">
<div className="register-form-v-spacing-small"></div> <Check text="Must be at least 8 characters" />
<Check text="Must contain one special character" /> <div className="register-form-v-spacing-small"></div>
</div> <Check text="Must contain one special character" />
<div className="login-form-v2-spacing" /> </div>
<div className="register-form-actions"> <div className="login-form-v2-spacing" />
<Button <div className="register-form-actions">
onClick={handleSubmit} <Button
level="primary" type="submit"
label="Get started" level="primary"
sx={{ width: "100%" }} label="Get started"
/> sx={{ width: "100%" }}
<div className="login-form-v-spacing" /> />
<Button <div className="login-form-v-spacing" />
disabled={true} <Button
level="secondary" disabled={true}
label="Sign up with Google" level="secondary"
sx={{ width: "100%", color: "#344054", fontWeight: "700" }} label="Sign up with Google"
img={<img className="google-enter" src={Google} alt="Google" />} sx={{ width: "100%", color: "#344054", fontWeight: "700" }}
/> img={<img className="google-enter" src={Google} alt="Google" />}
</div> />
</div> </div>
<div className="register-bottom-spacing"></div>
</form> </form>
<div className="register-bottom-spacing"></div>
</div> </div>
); );
}; };
-2
View File
@@ -1,4 +1,3 @@
const express = require("express");
const logger = require("../utils/logger"); const logger = require("../utils/logger");
require("dotenv").config(); require("dotenv").config();
const { const {
@@ -11,7 +10,6 @@ const {
editAlertBodyValidation, editAlertBodyValidation,
deleteAlertParamValidation, deleteAlertParamValidation,
} = require("../validation/joi"); } = require("../validation/joi");
const { get } = require("mongoose");
const SERVICE_NAME = "alerts"; const SERVICE_NAME = "alerts";
+2 -14
View File
@@ -45,18 +45,6 @@ const registerController = async (req, res, next) => {
return; 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 // Create a new user
try { try {
const newUser = await req.db.insertUser(req, res); const newUser = await req.db.insertUser(req, res);
@@ -101,8 +89,8 @@ const loginController = async (req, res, next) => {
// Compare password // Compare password
const match = await user.comparePassword(req.body.password); const match = await user.comparePassword(req.body.password);
if (!match) { if (match !== true) {
throw new Error("Password does not match!"); throw new Error("Incorrect password");
} }
// Remove password from user object. Should this be abstracted to DB layer? // Remove password from user object. Should this be abstracted to DB layer?
+4
View File
@@ -5,6 +5,7 @@ const Check = require("../models/Check");
const Alert = require("../models/Alert"); const Alert = require("../models/Alert");
const RecoveryToken = require("../models/RecoveryToken"); const RecoveryToken = require("../models/RecoveryToken");
const crypto = require("crypto"); const crypto = require("crypto");
const DUPLICATE_KEY_CODE = 11000; // MongoDB error code for duplicate key
const connect = async () => { const connect = async () => {
try { try {
@@ -34,6 +35,9 @@ const insertUser = async (req, res) => {
await newUser.save(); await newUser.save();
return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find return await UserModel.findOne({ _id: newUser._id }).select("-password"); // .select() doesn't work with create, need to save then find
} catch (error) { } catch (error) {
if (error.code === DUPLICATE_KEY_CODE) {
throw new Error("Email already exists");
}
throw error; throw error;
} }
}; };
-1
View File
@@ -4,7 +4,6 @@ const handleErrors = (error, req, res, next) => {
const status = error.status || 500; const status = error.status || 500;
const message = error.message || "Something went wrong"; const message = error.message || "Something went wrong";
const service = error.service || "Unknown service"; const service = error.service || "Unknown service";
logger.error(error.message, { service: service }); logger.error(error.message, { service: service });
res.status(status).json({ success: false, msg: message }); res.status(status).json({ success: false, msg: message });
}; };