From 797eacee22d6facf78d6f18c63b996806274c345 Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Mon, 15 Jul 2024 16:32:24 -0400 Subject: [PATCH 01/18] Refactored login page validation --- Client/src/Pages/Login/index.jsx | 118 +++++++++++++++------------- Client/src/Validation/validation.js | 107 +++++++++++++++++-------- 2 files changed, 137 insertions(+), 88 deletions(-) diff --git a/Client/src/Pages/Login/index.jsx b/Client/src/Pages/Login/index.jsx index c06f3b343..e8a13ed15 100644 --- a/Client/src/Pages/Login/index.jsx +++ b/Client/src/Pages/Login/index.jsx @@ -8,7 +8,7 @@ import CheckBox from "../../Components/Checkbox/Checkbox"; import Button from "../../Components/Button"; import Google from "../../assets/Images/Google.png"; import axiosInstance from "../../Utils/axiosConfig"; -import { loginValidation } from "../../Validation/validation"; +import { loginValidation, credentials } from "../../Validation/validation"; import { login } from "../../Features/Auth/authSlice"; import { useDispatch } from "react-redux"; import { createToast } from "../../Utils/toastUtils"; @@ -23,12 +23,11 @@ const Login = () => { "login-password-input": "password", }; - const [errors, setErrors] = useState({}); - const [form, setForm] = useState({ email: "", password: "", }); + const [errors, setErrors] = useState({}); useEffect(() => { axiosInstance @@ -43,71 +42,74 @@ const Login = () => { }); }, [navigate]); - useEffect(() => { - const { error } = loginValidation.validate(form, { - abortEarly: false, - }); - - if (error) { - // Creates an error object in the format { field: message } - const validationErrors = error.details.reduce((acc, err) => { - return { ...acc, [err.path[0]]: err.message }; - }, {}); - setErrors(validationErrors); - } else { - setErrors({}); - } - }, [form]); - const handleSubmit = async (e) => { e.preventDefault(); - try { - await loginValidation.validateAsync(form, { abortEarly: false }); + + const { error } = credentials.validate(form, { abortEarly: false }); + + if (error) { + // validation errors + const newErrors = {}; + error.details.forEach((err) => { + newErrors[err.path[0]] = err.message; + }); + setErrors(newErrors); + createToast({ + variant: "info", + body: + error.details && error.details.length > 0 + ? error.details[0].message + : "Error validating data.", + hasIcon: false, + }); + } else { const action = await dispatch(login(form)); - if (action.meta.requestStatus === "fulfilled") { + if (action.payload.success) { navigate("/monitors"); - } - if (action.meta.requestStatus === "rejected") { - const error = new Error("Request rejected"); - error.response = action.payload; - throw error; - } - } catch (error) { - if (error.name === "ValidationError") { - // validation errors createToast({ variant: "info", - body: - error && error.details && error.details.length > 0 - ? error.details[0].message - : "Error validating data.", - hasIcon: false, - }); - } else if (error.response) { - // dispatch errors - createToast({ - variant: "info", - body: error.response.msg, + body: "Welcome back! You're successfully logged in.", hasIcon: false, }); } else { - // unknown errors - createToast({ - variant: "info", - body: "Unknown error.", - hasIcon: false, - }); + if (action.payload) { + // dispatch errors + createToast({ + variant: "info", + body: action.payload.msg, + hasIcon: false, + }); + } else { + // unknown errors + createToast({ + variant: "info", + body: "Unknown error.", + hasIcon: false, + }); + } } } }; - const handleInput = (e) => { - const newForm = { ...form, [idMap[e.target.id]]: e.target.value }; - setForm(newForm); - }; + const handleChange = (event) => { + const { value, id } = event.target; + const name = idMap[id]; + setForm((prev) => ({ + ...prev, + [name]: value, + })); - const handleSignupClick = () => { - navigate("/register"); + const { error } = credentials.validate( + { [name]: value }, + { abortEarly: false } + ); + + setErrors((prev) => { + const prevErrors = { ...prev }; + if (error) prevErrors[name] = error.details[0].message; + else delete prevErrors[name]; + return prevErrors; + }); }; return ( @@ -126,9 +128,11 @@ const Login = () => { type="email" id="login-email-input" label="Email" + isRequired={true} placeholder="Enter your email" autoComplete="email" - onChange={handleInput} + value={form.email} + onChange={handleChange} error={errors.email} />
@@ -136,9 +140,11 @@ const Login = () => { type="password" id="login-password-input" label="Password" + isRequired={true} placeholder="Enter your password" autoComplete="current-password" - onChange={handleInput} + value={form.password} + onChange={handleChange} error={errors.password} />
diff --git a/Client/src/Validation/validation.js b/Client/src/Validation/validation.js index f662d550e..ce735a554 100644 --- a/Client/src/Validation/validation.js +++ b/Client/src/Validation/validation.js @@ -1,5 +1,78 @@ import joi from "joi"; +const nameSchema = joi + .string() + .max(50) + .trim() + .pattern(new RegExp("^[A-Za-z]+$")) + .messages({ + "string.empty": "Name is required", + "string.max": "Name must be less than 50 characters long", + "string.pattern.base": "Name must contain only letters", + }); + +const passwordSchema = joi + .string() + .trim() + .min(8) + .messages({ + "string.empty": "Password is required", + "string.min": "Password must be at least 8 characters long", + }) + .custom((value, helpers) => { + if (!/[A-Z]/.test(value)) { + return helpers.message( + "Password must contain at least one uppercase letter" + ); + } + if (!/[a-z]/.test(value)) { + return helpers.message( + "Password must contain at least one lowercase letter" + ); + } + if (!/\d/.test(value)) { + return helpers.message("Password must contain at least one number"); + } + if (!/[!@#$%^&*]/.test(value)) { + return helpers.message( + "Password must contain at least one special character" + ); + } + + return value; + }); + +const credentials = joi.object({ + firstname: nameSchema, + lastname: nameSchema, + email: joi + .string() + .trim() + .email({ tlds: { allow: false } }) + .messages({ + "string.empty": "Email is required", + "string.email": "Must be a valid email address", + }), + password: passwordSchema, + newPassword: passwordSchema, + confirm: joi + .string() + .trim() + .messages({ + "string.empty": "Password confirmation is required", + }) + .custom((value, helpers) => { + const { newPassword } = helpers.prefs.context; + if (value !== newPassword) { + return helpers.message("Passwords do not match"); + } + return value; + }), + role: joi.string().messages({ + "string.empty": "Role is required", + }), +}); + const registerValidation = joi.object({ firstname: joi.string().required().messages({ "string.empty": "First name is required", @@ -85,37 +158,6 @@ const editProfileValidation = joi.object({ }), }); -const passwordSchema = joi - .string() - .trim() - .min(8) - .messages({ - "string.empty": "*Password is required.", - "string.min": "*Password must be at least 8 characters long.", - }) - .custom((value, helpers) => { - if (!/[A-Z]/.test(value)) { - return helpers.message( - "*Password must contain at least one uppercase letter." - ); - } - if (!/[a-z]/.test(value)) { - return helpers.message( - "*Password must contain at least one lowercase letter." - ); - } - if (!/\d/.test(value)) { - return helpers.message("*Password must contain at least one number."); - } - if (!/[!@#$%^&*]/.test(value)) { - return helpers.message( - "*Password must contain at least one special character." - ); - } - - return value; - }); - const editPasswordValidation = joi.object({ // TBD - validation for current password ? password: passwordSchema, @@ -124,7 +166,7 @@ const editPasswordValidation = joi.object({ .string() .trim() .messages({ - "string.empty": "*Password confirmation is required.", + "string.empty": "Password confirmation is required", }) .custom((value, helpers) => { const { newPassword } = helpers.prefs.context; @@ -169,6 +211,7 @@ const imageValidation = joi.object({ }); export { + credentials, imageValidation, createMonitorValidation, registerValidation, From 5fbf5caec40d6c6ff029a619a64d87423f0ffae6 Mon Sep 17 00:00:00 2001 From: Daniel Cojocea Date: Mon, 15 Jul 2024 18:14:20 -0400 Subject: [PATCH 02/18] Restructured login page --- Client/src/Components/Button/index.jsx | 4 + Client/src/Pages/Login/index.css | 104 ++++++++++------------- Client/src/Pages/Login/index.jsx | 110 +++++++++++++++---------- Client/src/Utils/Theme.js | 1 + 4 files changed, 112 insertions(+), 107 deletions(-) diff --git a/Client/src/Components/Button/index.jsx b/Client/src/Components/Button/index.jsx index 539076483..fcf157f5e 100644 --- a/Client/src/Components/Button/index.jsx +++ b/Client/src/Components/Button/index.jsx @@ -63,6 +63,10 @@ const Button = ({ id, type, level, label, disabled, img, onClick, sx }) => { "&:focus": { outline: "none", }, + "& .MuiTouchRipple-root": { + pointerEvents: "none", + display: "none", + }, ...sx, }} > diff --git a/Client/src/Pages/Login/index.css b/Client/src/Pages/Login/index.css index ed6b4d1dc..bf5b8c435 100644 --- a/Client/src/Pages/Login/index.css +++ b/Client/src/Pages/Login/index.css @@ -3,78 +3,58 @@ display: flex; justify-content: center; align-items: center; - min-height: var(--env-var-height-1); + height: 100vh; } - -.login-form { - width: var(--env-var-width-2); - margin: 90px auto; +.login-page svg rect { + fill: none; } - -.login-form-header { - justify-content: center; - align-items: center; - text-align: center; -} - -#login-form-header-logo { - margin-bottom: 60px; -} - -.login-form-v-spacing { - height: var(--env-var-spacing-1); -} - -.login-form-v2-spacing { - height: var(--env-var-spacing-2); -} - -.login-form-v3-spacing { - height: var(--env-var-spacing-3); -} - -.login-form-heading { - font-size: var(--env-var-font-size-xlarge); - font-weight: 700; +.login-page h1.MuiTypography-root { + font-size: 30px; + font-weight: 600; color: var(--env-var-color-1); } +.login-page p.MuiTypography-root, +.login-page span, +.login-page button { + font-size: var(--env-var-font-size-medium); +} +.login-page p.MuiTypography-root { + color: var(--env-var-color-2); + opacity: 0.8; +} +.login-page button { + height: 34px; + border-radius: var(--env-var-radius-2); + line-height: 0; +} +.login-page span:not(.MuiTypography-root):not(.MuiButtonBase-root) { + color: var(--env-var-color-3); + cursor: pointer; + font-weight: 600; +} +.login-page .MuiFormControlLabel-root { + margin: 0; +} +.login-page .MuiFormControlLabel-root .MuiButtonBase-root { + margin-right: 5px; +} +.login-page .MuiFormControlLabel-root span:not(.Mui-checked) svg{ + fill: var(--env-var-color-4); +} +.login-page .MuiFormControlLabel-root svg{ + margin-bottom: 1px; +} +.login-page .MuiFormControlLabel-root .MuiTypography-root { + color: var(--env-var-color-5); + user-select: none; +} /* .login-form-inputs .field h3.MuiTypography-root{ font-weight: 600; } */ -.login-form-password-options { - display: flex; - align-items: center; - justify-content: space-between; -} - -.login-form-forgot-password { - color: var(--env-var-color-3); - font-weight: 600; - cursor: pointer; - font-size: var(--env-var-font-size-medium); -} - .google-enter { width: var(--env-var-img-width-1); height: var(--env-var-img-width-1); margin-right: 10px; -} - -.new-account-option { - display: flex; - justify-content: center; - align-items: center; - color: var(--env-var-color-2); - font-size: var(--env-var-font-size-medium); - font-weight: 600; -} - -.new-account-option-span { - margin-left: 5px; - color: var(--env-var-color-3); - font-size: var(--env-var-font-size-medium); - cursor: pointer; - font-weight: 700; -} +} \ No newline at end of file diff --git a/Client/src/Pages/Login/index.jsx b/Client/src/Pages/Login/index.jsx index e8a13ed15..aeea0d1e4 100644 --- a/Client/src/Pages/Login/index.jsx +++ b/Client/src/Pages/Login/index.jsx @@ -8,15 +8,24 @@ import CheckBox from "../../Components/Checkbox/Checkbox"; import Button from "../../Components/Button"; import Google from "../../assets/Images/Google.png"; import axiosInstance from "../../Utils/axiosConfig"; -import { loginValidation, credentials } from "../../Validation/validation"; +import { credentials } from "../../Validation/validation"; import { login } from "../../Features/Auth/authSlice"; import { useDispatch } from "react-redux"; import { createToast } from "../../Utils/toastUtils"; import Field from "../../Components/Inputs/Field"; +import { + Checkbox, + Divider, + FormControlLabel, + Stack, + Typography, +} from "@mui/material"; +import { useTheme } from "@emotion/react"; const Login = () => { const dispatch = useDispatch(); const navigate = useNavigate(); + const theme = useTheme(); const idMap = { "login-email-input": "email", @@ -115,15 +124,22 @@ const Login = () => { return (
-
-
-