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";
|
2024-08-09 13:33:54 -04:00
|
|
|
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";
|
2024-08-09 13:33:54 -04:00
|
|
|
import Button from "../../Components/Button";
|
|
|
|
|
import axiosInstance from "../../Utils/axiosConfig";
|
2024-07-13 14:54:06 -04:00
|
|
|
import Field from "../../Components/Inputs/Field";
|
2024-08-09 13:33:54 -04:00
|
|
|
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";
|
2024-08-09 13:33:54 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-07-02 15:54:49 -07:00
|
|
|
useEffect(() => {
|
2024-07-09 11:45:14 -07:00
|
|
|
axiosInstance
|
|
|
|
|
.get("/auth/users/admin")
|
2024-07-02 15:54:49 -07:00
|
|
|
.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) {
|
2024-06-24 13:03:42 -07:00
|
|
|
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;
|
|
|
|
|
});
|
2024-06-24 13:51:11 -07:00
|
|
|
};
|
2024-06-24 14:59:19 -07:00
|
|
|
|
2024-08-09 13:33:54 -04:00
|
|
|
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 (
|
2024-08-09 13:33:54 -04:00
|
|
|
<Stack className="login-page" overflow="hidden">
|
2024-07-17 17:44:29 -04:00
|
|
|
<img
|
|
|
|
|
className="background-pattern-svg"
|
|
|
|
|
src={background}
|
|
|
|
|
alt="background pattern"
|
|
|
|
|
/>
|
2024-08-09 13:33:54 -04:00
|
|
|
<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}
|
2024-08-09 13:33:54 -04:00
|
|
|
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 /> : ""}
|
2024-08-09 13:33:54 -04:00
|
|
|
</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 }}>
|
2024-08-09 13:33:54 -04:00
|
|
|
Log In
|
2024-07-18 17:40:38 -04:00
|
|
|
</Typography>
|
2024-08-09 13:33:54 -04:00
|
|
|
<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)` }}>
|
2024-07-13 13:11:03 -04:00
|
|
|
<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}
|
2024-07-13 13:11:03 -04:00
|
|
|
error={errors.email}
|
2024-05-22 17:41:24 +03:30
|
|
|
/>
|
2024-07-13 13:11:03 -04:00
|
|
|
<Field
|
|
|
|
|
type="password"
|
2024-06-06 17:41:09 -07:00
|
|
|
id="login-password-input"
|
2024-07-13 13:11:03 -04:00
|
|
|
label="Password"
|
2024-07-15 16:32:24 -04:00
|
|
|
isRequired={true}
|
2024-07-15 18:14:20 -04:00
|
|
|
placeholder="••••••••••"
|
2024-07-13 13:11:03 -04:00
|
|
|
autoComplete="current-password"
|
2024-07-15 16:32:24 -04:00
|
|
|
value={form.password}
|
|
|
|
|
onChange={handleChange}
|
2024-07-13 13:11:03 -04:00
|
|
|
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"
|
|
|
|
|
/>
|
2024-08-09 13:33:54 -04:00
|
|
|
<Typography
|
|
|
|
|
component="span"
|
|
|
|
|
onClick={() => navigate("/forgot-password")}
|
|
|
|
|
>
|
2024-07-16 14:29:59 -04:00
|
|
|
Forgot password
|
2024-08-01 20:12:30 -04:00
|
|
|
</Typography>
|
2024-07-15 18:14:20 -04:00
|
|
|
</Stack>
|
|
|
|
|
</Stack>
|
|
|
|
|
<Stack gap={theme.gap.ml} mt={theme.gap.large}>
|
2024-07-16 11:32:33 -04:00
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
level="primary"
|
2024-07-18 17:40:38 -04:00
|
|
|
label="Continue"
|
2024-07-16 11:32:33 -04:00
|
|
|
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}
|
2024-06-24 13:51:11 -07:00
|
|
|
>
|
2024-08-09 13:33:54 -04:00
|
|
|
<Typography sx={{ alignSelf: "center" }}>
|
2024-07-15 18:14:20 -04:00
|
|
|
Don't have an account?
|
|
|
|
|
</Typography>
|
2024-08-09 13:33:54 -04:00
|
|
|
<Typography
|
|
|
|
|
component="span"
|
2024-07-15 18:14:20 -04:00
|
|
|
onClick={() => {
|
|
|
|
|
navigate("/register");
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Sign up
|
2024-08-01 20:12:30 -04:00
|
|
|
</Typography>
|
2024-07-15 18:14:20 -04:00
|
|
|
</Stack>
|
|
|
|
|
</Stack>
|
2024-08-09 13:33:54 -04:00
|
|
|
</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;
|