Files
Checkmate/Client/src/Pages/Auth/Login.jsx
T

406 lines
11 KiB
React
Raw Normal View History

import { useState, useEffect, useRef } from "react";
2024-06-10 11:53:10 -07:00
import { useNavigate } from "react-router-dom";
2024-08-09 15:36:18 -04:00
import { Box, Stack, Typography } from "@mui/material";
import { useTheme } from "@emotion/react";
2024-07-15 18:14:20 -04:00
import { credentials } from "../../Validation/validation";
2024-06-10 10:38:00 -07:00
import { login } from "../../Features/Auth/authSlice";
import { useDispatch } from "react-redux";
2024-07-12 14:00:16 -04:00
import { createToast } from "../../Utils/toastUtils";
import Button from "../../Components/Button";
import axiosInstance from "../../Utils/axiosConfig";
import Field from "../../Components/Inputs/Field";
import background from "../../assets/Images/background_pattern_decorative.png";
import Logo from "../../assets/icons/bwu-icon.svg?react";
import Mail from "../../assets/icons/mail.svg?react";
2024-08-09 14:34:59 -04:00
import ArrowBackRoundedIcon from "@mui/icons-material/ArrowBackRounded";
import "./index.css";
2024-06-10 10:38:00 -07:00
2024-08-09 15:41:10 -04:00
/**
* Displays the initial landing page.
*
* @param {Object} props
* @param {Function} props.onContinue - Callback function to handle "Continue with Email" button click.
* @returns {JSX.Element}
2024-08-09 15:41:10 -04:00
*/
2024-08-09 15:36:18 -04:00
const LandingPage = ({ onContinue }) => {
const theme = useTheme();
return (
<>
<Stack gap={theme.gap.large} alignItems="center" textAlign="center">
<Box>
<Typography component="h1">Log In</Typography>
<Typography>We are pleased to see you again!</Typography>
</Box>
<Box width="100%">
<Button
level="secondary"
label="Continue with Email"
img={<Mail />}
onClick={onContinue}
sx={{
width: "100%",
"& svg": {
mr: theme.gap.small,
},
}}
/>
</Box>
<Box maxWidth={400}>
<Typography className="tos-p">
By continuing, you agree to our{" "}
<Typography component="span">Terms of Service</Typography> and{" "}
<Typography component="span">Privacy Policy.</Typography>
</Typography>
</Box>
</Stack>
</>
);
};
2024-08-09 15:41:10 -04:00
/**
* Renders the first step of the login process.
*
* @param {Object} props
* @param {Object} props.form - Form state object.
* @param {Object} props.errors - Object containing form validation errors.
* @param {Function} props.onSubmit - Callback function to handle form submission.
* @param {Function} props.onChange - Callback function to handle form input changes.
* @param {Function} props.onBack - Callback function to handle "Back" button click.
* @returns {JSX.Element}
*/
2024-08-09 15:36:18 -04:00
const StepOne = ({ form, errors, onSubmit, onChange, onBack }) => {
const theme = useTheme();
const inputRef = useRef(null);
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
2024-08-09 15:36:18 -04:00
return (
<>
<Stack gap={theme.gap.large} textAlign="center">
<Box>
<Typography component="h1">Log In</Typography>
<Typography>Enter your email address</Typography>
</Box>
<Box textAlign="left">
<form noValidate spellCheck={false} onSubmit={onSubmit}>
<Field
type="email"
id="login-email-input"
label="Email"
isRequired={true}
2024-08-10 17:46:35 -04:00
placeholder="jordan.ellis@domain.com"
2024-08-09 15:36:18 -04:00
autoComplete="email"
value={form.email}
onChange={onChange}
error={errors.email}
ref={inputRef}
2024-08-09 15:36:18 -04:00
/>
</form>
</Box>
<Stack direction="row" justifyContent="space-between">
<Button
level="secondary"
label="Back"
animate="slideLeft"
img={<ArrowBackRoundedIcon />}
onClick={onBack}
sx={{
mb: theme.gap.medium,
px: theme.gap.ml,
"& svg.MuiSvgIcon-root": {
mr: theme.gap.xs,
},
}}
2024-08-10 17:13:31 -04:00
props={{ tabIndex: -1 }}
2024-08-09 15:36:18 -04:00
/>
<Button
level="primary"
label="Continue"
onClick={onSubmit}
disabled={errors.email && true}
sx={{ width: "30%" }}
/>
</Stack>
</Stack>
</>
);
};
2024-08-09 15:41:10 -04:00
/**
* Renders the second step of the login process, including a password input field.
*
* @param {Object} props
* @param {Object} props.form - Form state object.
* @param {Object} props.errors - Object containing form validation errors.
* @param {Function} props.onSubmit - Callback function to handle form submission.
* @param {Function} props.onChange - Callback function to handle form input changes.
* @param {Function} props.onBack - Callback function to handle "Back" button click.
* @returns {JSX.Element}
*/
2024-08-09 15:36:18 -04:00
const StepTwo = ({ form, errors, onSubmit, onChange, onBack }) => {
const theme = useTheme();
2024-08-09 16:16:21 -04:00
const navigate = useNavigate();
const inputRef = useRef(null);
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
2024-08-09 15:36:18 -04:00
return (
<>
<Stack gap={theme.gap.large} textAlign="center">
<Box>
<Typography component="h1">Log In</Typography>
<Typography>Enter your password</Typography>
</Box>
<Box textAlign="left">
<form noValidate spellCheck={false} onSubmit={onSubmit}>
<Field
type="password"
id="login-password-input"
label="Password"
isRequired={true}
placeholder="••••••••••"
autoComplete="current-password"
value={form.password}
onChange={onChange}
error={errors.password}
ref={inputRef}
2024-08-09 15:36:18 -04:00
/>
</form>
</Box>
<Stack direction="row" justifyContent="space-between">
<Button
level="secondary"
label="Back"
animate="slideLeft"
img={<ArrowBackRoundedIcon />}
onClick={onBack}
sx={{
mb: theme.gap.medium,
px: theme.gap.ml,
"& svg.MuiSvgIcon-root": {
mr: theme.gap.xs,
},
}}
2024-08-10 17:13:31 -04:00
props={{ tabIndex: -1 }}
2024-08-09 15:36:18 -04:00
/>
<Button
level="primary"
label="Continue"
onClick={onSubmit}
2024-08-09 16:16:21 -04:00
disabled={errors.password && true}
2024-08-09 15:36:18 -04:00
sx={{ width: "30%" }}
/>
</Stack>
2024-08-09 16:16:21 -04:00
<Box textAlign="center">
<Typography className="forgot-p" display="inline-block">
Forgot password?
</Typography>
<Typography
component="span"
ml={theme.gap.xs}
sx={{ userSelect: "none" }}
onClick={() => navigate("/forgot-password")}
>
Reset password
</Typography>
</Box>
2024-08-09 15:36:18 -04:00
</Stack>
</>
);
};
2024-05-06 12:29:36 -04:00
const Login = () => {
2024-06-10 10:38:00 -07:00
const dispatch = useDispatch();
2024-06-10 11:53:10 -07:00
const navigate = useNavigate();
2024-07-15 18:14:20 -04:00
const theme = useTheme();
2024-06-10 10:38:00 -07:00
2024-06-06 17:41:09 -07:00
const idMap = {
"login-email-input": "email",
"login-password-input": "password",
};
const [form, setForm] = useState({
email: "",
password: "",
});
2024-07-15 16:32:24 -04:00
const [errors, setErrors] = useState({});
2024-08-09 15:41:10 -04:00
const [step, setStep] = useState(0);
2024-06-06 17:41:09 -07:00
2024-08-10 18:57:30 -04:00
useEffect(() => {
axiosInstance
.get("/auth/users/admin")
.then((response) => {
if (response.data.data === false) {
navigate("/register");
}
})
.catch((error) => {
console.log(error);
});
}, [navigate]);
2024-07-15 16:32:24 -04:00
const handleChange = (event) => {
const { value, id } = event.target;
const name = idMap[id];
setForm((prev) => ({
...prev,
[name]: value,
}));
const { error } = credentials.validate(
{ [name]: value },
{ abortEarly: false }
);
2024-06-06 17:41:09 -07:00
2024-07-15 16:32:24 -04:00
setErrors((prev) => {
const prevErrors = { ...prev };
if (error) prevErrors[name] = error.details[0].message;
else delete prevErrors[name];
return prevErrors;
});
};
2024-08-09 15:36:18 -04:00
const handleSubmit = async (event) => {
event.preventDefault();
2024-08-09 14:34:59 -04:00
2024-08-09 15:36:18 -04:00
if (step === 1) {
2024-08-09 14:34:59 -04:00
const { error } = credentials.validate(
{ email: form.email },
{ abortEarly: false }
);
if (error) {
setErrors((prev) => ({ ...prev, email: error.details[0].message }));
2024-08-09 15:36:18 -04:00
createToast({ body: error.details[0].message });
2024-08-09 14:34:59 -04:00
} else {
setStep(2);
}
2024-08-09 15:36:18 -04:00
} else if (step === 2) {
const { error } = credentials.validate(form, { abortEarly: false });
2024-08-09 14:34:59 -04:00
2024-08-09 15:36:18 -04:00
if (error) {
// validation errors
const newErrors = {};
error.details.forEach((err) => {
newErrors[err.path[0]] = err.message;
});
setErrors(newErrors);
createToast({
body:
error.details && error.details.length > 0
? error.details[0].message
: "Error validating data.",
});
} else {
const action = await dispatch(login(form));
if (action.payload.success) {
navigate("/monitors");
createToast({
body: "Welcome back! You're successfully logged in.",
});
} else {
2024-08-09 16:16:21 -04:00
setErrors({
password: "The password you provided does not match our records",
});
2024-08-09 15:36:18 -04:00
if (action.payload) {
// dispatch errors
createToast({
body: action.payload.msg,
});
} else {
// unknown errors
createToast({
body: "Unknown error.",
});
}
}
}
}
2024-08-09 14:34:59 -04:00
};
2024-05-22 17:41:24 +03:30
return (
2024-08-10 17:13:31 -04:00
<Stack className="login-page auth" overflow="hidden">
2024-07-17 17:44:29 -04:00
<img
className="background-pattern-svg"
src={background}
alt="background pattern"
/>
<Stack
direction="row"
alignItems="center"
px={theme.gap.large}
gap={theme.gap.small}
>
<Logo style={{ borderRadius: theme.shape.borderRadius }} />
2024-08-10 17:13:31 -04:00
<Typography sx={{ userSelect: "none" }}>BlueWave Uptime</Typography>
</Stack>
<Stack
2024-08-09 14:34:59 -04:00
width="100%"
maxWidth={600}
flex={1}
justifyContent="center"
p={theme.gap.xl}
pb={theme.gap.triplexl}
mx="auto"
sx={{
"& > .MuiStack-root": {
border: 1,
borderRadius: theme.shape.borderRadius,
borderColor: theme.palette.otherColors.graishWhite,
backgroundColor: theme.palette.otherColors.white,
padding: {
xs: theme.gap.large,
sm: theme.gap.xl,
},
},
}}
>
2024-08-09 15:36:18 -04:00
{step === 0 ? (
<LandingPage onContinue={() => setStep(1)} />
) : step === 1 ? (
<StepOne
form={form}
errors={errors}
onSubmit={handleSubmit}
onChange={handleChange}
onBack={() => setStep(0)}
/>
) : (
step === 2 && (
<StepTwo
form={form}
errors={errors}
onSubmit={handleSubmit}
onChange={handleChange}
onBack={() => setStep(1)}
/>
)
)}
</Stack>
<Box textAlign="center" p={theme.gap.large}>
2024-08-09 16:16:21 -04:00
<Typography display="inline-block">Don't have an account? —</Typography>
<Typography
component="span"
ml={theme.gap.xs}
onClick={() => {
navigate("/register");
}}
sx={{ userSelect: "none" }}
>
Sign Up
</Typography>
</Box>
</Stack>
2024-05-22 17:41:24 +03:30
);
};
2024-05-06 12:29:36 -04:00
2024-05-22 17:41:24 +03:30
export default Login;