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

355 lines
10 KiB
React
Raw Normal View History

2024-06-10 10:38:00 -07:00
import { useState, useEffect } from "react";
2024-06-10 11:53:10 -07:00
import { useNavigate } from "react-router-dom";
2024-08-09 14:34:59 -04:00
import { Box, Divider, FormControl, 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-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-06-06 17:41:09 -07: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 handleSubmit = async (e) => {
e.preventDefault();
const { error } = credentials.validate(form, { abortEarly: false });
2024-06-10 10:38:00 -07:00
if (error) {
2024-07-15 16:32:24 -04:00
// 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.",
});
2024-06-10 11:53:10 -07:00
} else {
const action = await dispatch(login(form));
2024-07-15 16:32:24 -04:00
if (action.payload.success) {
navigate("/monitors");
2024-07-12 14:00:16 -04:00
createToast({
2024-07-15 16:32:24 -04:00
body: "Welcome back! You're successfully logged in.",
2024-07-12 14:00:16 -04:00
});
2024-06-10 11:53:10 -07:00
} else {
2024-07-15 16:32:24 -04:00
if (action.payload) {
// dispatch errors
createToast({
body: action.payload.msg,
});
} else {
// unknown errors
createToast({
body: "Unknown error.",
});
}
2024-06-10 11:53:10 -07:00
}
2024-06-10 10:38:00 -07:00
}
};
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;
});
};
const [step, setStep] = useState(0);
const LandingPage = () => {
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={() => setStep(1)}
sx={{
width: "100%",
"& svg": {
mr: theme.gap.small,
},
}}
/>
{/* <Divider sx={{ marginY: theme.spacing(1) }}>or</Divider>
<Button
level="secondary"
label="Continue with Google"
onClick={() => setStep(1)}
sx={{ width: "100%" }}
/> */}
</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 14:34:59 -04:00
const StepOne = () => {
const submit = (event) => {
event.preventDefault();
const { error } = credentials.validate(
{ email: form.email },
{ abortEarly: false }
);
if (error) {
setErrors((prev) => ({ ...prev, email: error.details[0].message }));
createToast({ body: "Invalid email address." });
} else {
setStep(2);
}
};
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 onSubmit={submit}>
<Field
type="email"
id="login-email-input"
label="Email"
isRequired={true}
placeholder="john.doe@domain.com"
autoComplete="email"
value={form.email}
onChange={handleChange}
error={errors.email}
/>
</form>
</Box>
<Stack direction="row" justifyContent="space-between">
<Button
level="secondary"
label="Back"
animate="slideLeft"
img={<ArrowBackRoundedIcon />}
onClick={() => setStep(0)}
sx={{
mb: theme.gap.medium,
px: theme.gap.ml,
"& svg.MuiSvgIcon-root": {
mr: theme.gap.xs,
},
}}
/>
<Button
level="primary"
label="Continue"
onClick={submit}
disabled={errors.email && true}
sx={{ width: "30%" }}
/>
</Stack>
</Stack>
</>
);
};
2024-05-22 17:41:24 +03:30
return (
<Stack className="login-page" 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 }} />
<Typography>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 14:34:59 -04:00
{step === 0 ? <LandingPage /> : step === 1 ? <StepOne /> : ""}
</Stack>
<Box textAlign="center" p={theme.gap.large}>
<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>
{/* <form className="login-form" onSubmit={handleSubmit}>
2024-07-18 17:40:38 -04:00
<Stack gap={theme.gap.small} alignItems="center">
<Typography component="h1" sx={{ mt: theme.gap.xl }}>
Log In
2024-07-18 17:40:38 -04:00
</Typography>
<Typography>We are pleased to see you again!</Typography>
2024-07-18 17:40:38 -04:00
</Stack>
<Stack gap={theme.gap.large} sx={{ mt: `calc(${theme.gap.ml}*2)` }}>
<Field
type="email"
id="login-email-input"
label="Email"
2024-07-15 16:32:24 -04:00
isRequired={true}
2024-05-22 17:41:24 +03:30
placeholder="Enter your email"
2024-06-12 13:51:28 -07:00
autoComplete="email"
2024-07-15 16:32:24 -04:00
value={form.email}
onChange={handleChange}
error={errors.email}
2024-05-22 17:41:24 +03:30
/>
<Field
type="password"
2024-06-06 17:41:09 -07:00
id="login-password-input"
label="Password"
2024-07-15 16:32:24 -04:00
isRequired={true}
2024-07-15 18:14:20 -04:00
placeholder="••••••••••"
autoComplete="current-password"
2024-07-15 16:32:24 -04:00
value={form.password}
onChange={handleChange}
error={errors.password}
2024-05-22 17:41:24 +03:30
/>
2024-07-15 18:14:20 -04:00
<Stack direction="row" justifyContent="space-between">
<FormControlLabel
control={
<Checkbox
size="small"
sx={{
padding: 0,
"& .MuiTouchRipple-root": {
pointerEvents: "none",
display: "none",
},
}}
/>
}
label="Remember me"
/>
<Typography
component="span"
onClick={() => navigate("/forgot-password")}
>
2024-07-16 14:29:59 -04:00
Forgot password
</Typography>
2024-07-15 18:14:20 -04:00
</Stack>
</Stack>
<Stack gap={theme.gap.ml} mt={theme.gap.large}>
<Button
type="submit"
level="primary"
2024-07-18 17:40:38 -04:00
label="Continue"
disabled={Object.keys(errors).length !== 0 && true}
/>
2024-07-15 18:14:20 -04:00
<Stack
direction="row"
justifyContent="center"
gap="5px"
mt={theme.gap.ml}
>
<Typography sx={{ alignSelf: "center" }}>
2024-07-15 18:14:20 -04:00
Don't have an account?
</Typography>
<Typography
component="span"
2024-07-15 18:14:20 -04:00
onClick={() => {
navigate("/register");
}}
>
Sign up
</Typography>
2024-07-15 18:14:20 -04:00
</Stack>
</Stack>
</form> */}
</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;