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

215 lines
5.8 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-05-22 17:41:24 +03:30
import "./index.css";
2024-07-13 10:52:50 +03:30
import Logomark from "../../assets/Images/bwl-logo-2.svg?react";
2024-05-22 17:41:24 +03:30
import Button from "../../Components/Button";
import Google from "../../assets/Images/Google.png";
2024-07-17 17:44:29 -04:00
import background from "../../assets/Images/background_pattern_decorative.png";
import axiosInstance from "../../Utils/axiosConfig";
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 Field from "../../Components/Inputs/Field";
2024-07-15 18:14:20 -04:00
import {
Checkbox,
Divider,
FormControlLabel,
Stack,
Typography,
} from "@mui/material";
import { useTheme } from "@emotion/react";
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({
variant: "info",
body:
error.details && error.details.length > 0
? error.details[0].message
: "Error validating data.",
hasIcon: false,
});
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({
variant: "info",
2024-07-15 16:32:24 -04:00
body: "Welcome back! You're successfully logged in.",
2024-07-12 14:00:16 -04:00
hasIcon: false,
});
2024-06-10 11:53:10 -07:00
} else {
2024-07-15 16:32:24 -04:00
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,
});
}
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-05-22 17:41:24 +03:30
return (
<div className="login-page">
2024-07-17 17:44:29 -04:00
<img
className="background-pattern-svg"
src={background}
alt="background pattern"
/>
2024-07-15 19:29:02 -04:00
<form className="login-form" onSubmit={handleSubmit}>
2024-07-15 18:14:20 -04:00
<Stack gap={theme.gap.large} direction="column">
2024-07-15 19:29:02 -04:00
<Logomark alt="BlueWave Uptime Icon" />
2024-07-15 18:14:20 -04:00
<Button
level="secondary"
label="Sign in with Google"
sx={{ fontWeight: "600", mt: theme.gap.xxl }}
img={<img className="google-enter" src={Google} alt="Google" />}
/>
<Divider>
<Typography>or</Typography>
</Divider>
<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"
/>
2024-07-16 14:29:59 -04:00
<span onClick={() => navigate("/forgot-password")}>
Forgot password
</span>
2024-07-15 18:14:20 -04:00
</Stack>
</Stack>
<Stack gap={theme.gap.ml} mt={theme.gap.large}>
<Button
type="submit"
level="primary"
label="Sign in"
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-07-15 18:14:20 -04:00
<Typography component="p" sx={{ alignSelf: "center" }}>
Don't have an account?
</Typography>
<span
onClick={() => {
navigate("/register");
}}
>
Sign up
</span>
</Stack>
</Stack>
2024-06-12 13:49:38 -07:00
</form>
2024-05-22 17:41:24 +03:30
</div>
);
};
2024-05-06 12:29:36 -04:00
2024-05-22 17:41:24 +03:30
export default Login;