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

530 lines
15 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-31 17:37:55 -04:00
import { Box, Button, 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, useSelector } from "react-redux";
2024-07-12 14:00:16 -04:00
import { createToast } from "../../Utils/toastUtils";
2024-08-18 17:42:49 -07:00
import { networkService } from "../../main";
import Field from "../../Components/Inputs/Field";
2024-09-06 17:14:23 -04:00
import Background from "../../assets/Images/background-grid.svg?react";
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 PropTypes from "prop-types";
import { logger } from "../../Utils/Logger";
import "./index.css";
2024-09-06 11:51:55 -07:00
const DEMO = import.meta.env.VITE_APP_DEMO;
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
2024-08-27 20:11:59 -04:00
gap={{ xs: theme.spacing(8), sm: theme.spacing(12) }}
alignItems="center"
textAlign="center"
>
2024-08-09 15:36:18 -04:00
<Box>
<Typography component="h1">Log In</Typography>
<Typography>We are pleased to see you again!</Typography>
</Box>
<Box width="100%">
<Button
2024-08-31 17:37:55 -04:00
variant="outlined"
color="info"
2024-08-09 15:36:18 -04:00
onClick={onContinue}
sx={{
width: "100%",
"& svg": {
2024-08-27 20:11:59 -04:00
mr: theme.spacing(4),
2024-08-31 17:37:55 -04:00
"& path": {
stroke: theme.palette.other.icon,
},
2024-08-09 15:36:18 -04:00
},
}}
2024-08-31 17:37:55 -04:00
>
<Mail />
Continue with Email
</Button>
2024-08-09 15:36:18 -04:00
</Box>
<Box maxWidth={400}>
<Typography className="tos-p">
By continuing, you agree to our{" "}
2024-08-21 10:58:40 -07:00
<Typography
component="span"
onClick={() => {
window.open(
"https://bluewavelabs.ca/terms-of-service-open-source",
2024-08-22 12:35:38 -04:00
"_blank",
"noreferrer"
2024-08-21 10:58:40 -07:00
);
}}
2024-08-27 20:11:59 -04:00
sx={{
"&:hover": {
color: theme.palette.text.tertiary,
},
}}
2024-08-21 10:58:40 -07:00
>
Terms of Service
</Typography>{" "}
and{" "}
<Typography
component="span"
onClick={() => {
2024-08-22 12:35:38 -04:00
window.open(
"https://bluewavelabs.ca/privacy-policy-open-source",
"_blank",
"noreferrer"
);
2024-08-21 10:58:40 -07:00
}}
2024-08-27 20:11:59 -04:00
sx={{
"&:hover": {
color: theme.palette.text.tertiary,
},
}}
2024-08-21 10:58:40 -07:00
>
Privacy Policy.
</Typography>
2024-08-09 15:36:18 -04:00
</Typography>
</Box>
</Stack>
</>
);
};
LandingPage.propTypes = {
onContinue: PropTypes.func.isRequired,
};
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 (
<>
2024-08-27 20:11:59 -04:00
<Stack
gap={{ xs: theme.spacing(8), sm: theme.spacing(12) }}
textAlign="center"
>
2024-08-09 15:36:18 -04:00
<Box>
<Typography component="h1">Log In</Typography>
<Typography>Enter your email address</Typography>
</Box>
<Box textAlign="left" mb={theme.spacing(5)}>
2024-08-09 15:36:18 -04:00
<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}
2024-08-23 10:36:02 -07:00
onInput={(e) => (e.target.value = e.target.value.toLowerCase())}
2024-08-09 15:36:18 -04:00
onChange={onChange}
error={errors.email}
ref={inputRef}
2024-08-09 15:36:18 -04:00
/>
</form>
</Box>
<Stack direction="row" justifyContent="space-between">
<Button
2024-08-31 17:37:55 -04:00
variant="outlined"
color="info"
2024-08-09 15:36:18 -04:00
onClick={onBack}
sx={{
2024-08-31 17:37:55 -04:00
px: theme.spacing(5),
2024-08-09 15:36:18 -04:00
"& svg.MuiSvgIcon-root": {
2024-08-31 17:37:55 -04:00
mr: theme.spacing(3),
2024-08-09 15:36:18 -04:00
},
}}
2024-08-10 17:13:31 -04:00
props={{ tabIndex: -1 }}
2024-08-31 17:37:55 -04:00
>
<ArrowBackRoundedIcon />
Back
</Button>
2024-08-09 15:36:18 -04:00
<Button
2024-08-31 17:37:55 -04:00
variant="contained"
color="primary"
2024-08-09 15:36:18 -04:00
onClick={onSubmit}
disabled={errors.email && true}
sx={{ width: "30%" }}
2024-08-31 17:37:55 -04:00
>
Continue
</Button>
2024-08-09 15:36:18 -04:00
</Stack>
</Stack>
</>
);
};
StepOne.propTypes = {
form: PropTypes.object.isRequired,
errors: PropTypes.object.isRequired,
onSubmit: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
onBack: PropTypes.func.isRequired,
};
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
2024-08-12 14:06:01 -04:00
const handleNavigate = () => {
if (form.email !== "" && !errors.email) {
sessionStorage.setItem("email", form.email);
}
navigate("/forgot-password");
};
2024-08-09 15:36:18 -04:00
return (
<>
2024-08-27 20:11:59 -04:00
<Stack
gap={{ xs: theme.spacing(8), sm: theme.spacing(12) }}
2024-08-31 17:37:55 -04:00
position="relative"
2024-08-27 20:11:59 -04:00
textAlign="center"
>
2024-08-09 15:36:18 -04:00
<Box>
<Typography component="h1">Log In</Typography>
<Typography>Enter your password</Typography>
</Box>
<Box textAlign="left" mb={theme.spacing(5)}>
2024-08-09 15:36:18 -04:00
<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
2024-08-31 17:37:55 -04:00
variant="outlined"
color="info"
2024-08-09 15:36:18 -04:00
onClick={onBack}
sx={{
2024-08-31 17:37:55 -04:00
px: theme.spacing(5),
2024-08-09 15:36:18 -04:00
"& svg.MuiSvgIcon-root": {
2024-08-31 17:37:55 -04:00
mr: theme.spacing(3),
2024-08-09 15:36:18 -04:00
},
}}
2024-08-10 17:13:31 -04:00
props={{ tabIndex: -1 }}
2024-08-31 17:37:55 -04:00
>
<ArrowBackRoundedIcon />
Back
</Button>
2024-08-09 15:36:18 -04:00
<Button
2024-08-31 17:37:55 -04:00
variant="contained"
color="primary"
2024-08-09 15:36:18 -04:00
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%" }}
2024-08-31 17:37:55 -04:00
>
Continue
</Button>
2024-08-09 15:36:18 -04:00
</Stack>
2024-08-31 17:37:55 -04:00
<Box
textAlign="center"
sx={{
position: "absolute",
2024-09-06 17:14:23 -04:00
top: "104%",
2024-08-31 17:37:55 -04:00
left: "50%",
2024-09-06 17:14:23 -04:00
transform: "translateX(-50%)",
2024-08-31 17:37:55 -04:00
}}
>
<Typography
className="forgot-p"
display="inline-block"
color={theme.palette.primary.main}
>
2024-08-09 16:16:21 -04:00
Forgot password?
</Typography>
<Typography
component="span"
color={theme.palette.primary.main}
2024-08-27 20:11:59 -04:00
ml={theme.spacing(2)}
2024-08-09 16:16:21 -04:00
sx={{ userSelect: "none" }}
2024-08-12 14:06:01 -04:00
onClick={handleNavigate}
2024-08-09 16:16:21 -04:00
>
Reset password
</Typography>
</Box>
2024-08-09 15:36:18 -04:00
</Stack>
</>
);
};
StepTwo.propTypes = {
form: PropTypes.object.isRequired,
errors: PropTypes.object.isRequired,
onSubmit: PropTypes.func.isRequired,
onChange: PropTypes.func.isRequired,
onBack: PropTypes.func.isRequired,
};
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
const authState = useSelector((state) => state.auth);
const { authToken } = authState;
2024-06-06 17:41:09 -07:00
const idMap = {
"login-email-input": "email",
"login-password-input": "password",
};
const [form, setForm] = useState({
2024-09-06 11:51:55 -07:00
email: DEMO !== undefined ? "uptimedemo@demo.com" : "",
password: DEMO !== undefined ? "Demouser1!" : "",
2024-06-06 17:41:09 -07:00
});
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(() => {
if (authToken) {
navigate("/monitors");
return;
}
2024-08-18 17:42:49 -07:00
networkService
2024-08-26 10:00:34 -07:00
.doesSuperAdminExist()
2024-08-10 18:57:30 -04:00
.then((response) => {
if (response.data.data === false) {
navigate("/register");
}
})
.catch((error) => {
logger.error(error);
2024-08-10 18:57:30 -04:00
});
}, [authToken, 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 {
if (action.payload) {
if (action.payload.msg === "Incorrect password")
setErrors({
password:
"The password you provided does not match our records",
});
2024-08-09 15:36:18 -04:00
// 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-27 20:11:59 -04:00
<Stack
className="login-page auth"
overflow="hidden"
sx={{
"& h1": {
color: theme.palette.primary.main,
2024-08-27 20:11:59 -04:00
fontWeight: 600,
2024-09-06 17:14:23 -04:00
fontSize: 28,
2024-08-27 20:11:59 -04:00
},
2024-09-06 17:14:23 -04:00
"& p": { fontSize: 14, color: theme.palette.text.accent },
"& span": { fontSize: "inherit" },
2024-08-27 20:11:59 -04:00
}}
>
<Box
2024-07-17 17:44:29 -04:00
className="background-pattern-svg"
2024-09-06 17:14:23 -04:00
sx={{
"& svg g g:last-of-type path": {
stroke: theme.palette.border.light,
},
}}
>
<Background style={{ width: "100%" }} />
</Box>
<Stack
direction="row"
alignItems="center"
2024-08-27 20:11:59 -04:00
px={theme.spacing(12)}
gap={theme.spacing(4)}
>
<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"
2024-08-27 20:11:59 -04:00
px={{ xs: theme.spacing(12), lg: theme.spacing(20) }}
pb={theme.spacing(20)}
mx="auto"
sx={{
"& > .MuiStack-root": {
border: 1,
2024-08-27 20:11:59 -04:00
borderRadius: theme.spacing(5),
borderColor: theme.palette.border.light,
backgroundColor: theme.palette.background.main,
padding: {
2024-08-27 20:11:59 -04:00
xs: theme.spacing(12),
sm: theme.spacing(20),
},
},
}}
>
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>
2024-08-27 20:11:59 -04:00
<Box textAlign="center" p={theme.spacing(12)}>
2024-08-20 10:47:27 -07:00
<Typography display="inline-block">
Don&apos;t have an account? —
</Typography>
<Typography
component="span"
color={theme.palette.primary.main}
2024-08-27 20:11:59 -04:00
ml={theme.spacing(2)}
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;