2025-06-11 10:20:14 +08:00
|
|
|
// Components
|
|
|
|
|
import Stack from "@mui/material/Stack";
|
|
|
|
|
import Button from "@mui/material/Button";
|
|
|
|
|
import TextInput from "../../../Components/Inputs/TextInput";
|
|
|
|
|
import { PasswordEndAdornment } from "../../../Components/Inputs/TextInput/Adornments";
|
|
|
|
|
import { loginCredentials } from "../../../Validation/validation";
|
2025-06-13 14:54:59 +08:00
|
|
|
import TextLink from "../../../Components/TextLink";
|
2025-08-06 17:17:44 -04:00
|
|
|
import AuthPageWrapper from "../components/AuthPageWrapper";
|
2025-06-11 10:20:14 +08:00
|
|
|
// Utils
|
|
|
|
|
import { login } from "../../../Features/Auth/authSlice";
|
|
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
|
import { useDispatch } from "react-redux";
|
|
|
|
|
import { useState } from "react";
|
|
|
|
|
import { useTheme } from "@mui/material/styles";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
import { createToast } from "../../../Utils/toastUtils";
|
|
|
|
|
|
|
|
|
|
const Login = () => {
|
|
|
|
|
// Local state
|
|
|
|
|
const [form, setForm] = useState({
|
|
|
|
|
email: "",
|
|
|
|
|
password: "",
|
|
|
|
|
});
|
|
|
|
|
const [errors, setErrors] = useState({
|
|
|
|
|
email: "",
|
|
|
|
|
password: "",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Hooks
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
|
const navigate = useNavigate();
|
|
|
|
|
|
|
|
|
|
// Handlers
|
|
|
|
|
const onChange = (e) => {
|
2025-06-11 12:32:03 +08:00
|
|
|
let { name, value } = e.target;
|
|
|
|
|
if (name === "email") {
|
|
|
|
|
value = value.toLowerCase();
|
|
|
|
|
}
|
2025-06-11 10:20:14 +08:00
|
|
|
const updatedForm = { ...form, [name]: value };
|
|
|
|
|
const { error } = loginCredentials.validate({ [name]: value });
|
|
|
|
|
setForm(updatedForm);
|
|
|
|
|
setErrors((prev) => ({
|
|
|
|
|
...prev,
|
|
|
|
|
[name]: error?.details?.[0]?.message || "",
|
|
|
|
|
}));
|
|
|
|
|
};
|
|
|
|
|
const onSubmit = async (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const toSubmit = { ...form };
|
|
|
|
|
const { error } = loginCredentials.validate(toSubmit, { abortEarly: false });
|
|
|
|
|
if (error) {
|
|
|
|
|
const formErrors = {};
|
|
|
|
|
for (const err of error.details) {
|
|
|
|
|
formErrors[err.path[0]] = err.message;
|
|
|
|
|
}
|
|
|
|
|
setErrors(formErrors);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const action = await dispatch(login(form));
|
|
|
|
|
if (action.payload.success) {
|
|
|
|
|
navigate("/uptime");
|
|
|
|
|
createToast({
|
|
|
|
|
body: t("auth.login.toasts.success"),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
if (action.payload) {
|
|
|
|
|
if (action.payload.msg === "Incorrect password")
|
|
|
|
|
setErrors({
|
|
|
|
|
password: t("auth.login.errors.password.incorrect"),
|
|
|
|
|
});
|
|
|
|
|
// dispatch errors
|
|
|
|
|
createToast({
|
|
|
|
|
body: t("auth.login.toasts.incorrectPassword"),
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
// unknown errors
|
|
|
|
|
createToast({
|
|
|
|
|
body: t("common.toasts.unknownError"),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
return (
|
2025-08-06 17:17:44 -04:00
|
|
|
<AuthPageWrapper
|
|
|
|
|
welcome={t("auth.login.welcome")}
|
|
|
|
|
heading={t("auth.login.heading")}
|
2025-06-11 10:20:14 +08:00
|
|
|
>
|
2025-07-31 16:32:30 -04:00
|
|
|
<Stack
|
2025-08-06 17:17:44 -04:00
|
|
|
component="form"
|
|
|
|
|
width="100%"
|
|
|
|
|
padding={theme.spacing(8)}
|
|
|
|
|
gap={theme.spacing(12)}
|
|
|
|
|
onSubmit={onSubmit}
|
2025-07-31 16:32:30 -04:00
|
|
|
sx={{
|
|
|
|
|
width: {
|
2025-08-06 17:17:44 -04:00
|
|
|
sm: "80%",
|
|
|
|
|
md: "70%",
|
|
|
|
|
lg: "65%",
|
|
|
|
|
xl: "65%",
|
2025-07-31 16:32:30 -04:00
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-08-06 17:17:44 -04:00
|
|
|
<TextInput
|
|
|
|
|
type="email"
|
|
|
|
|
name="email"
|
|
|
|
|
label={t("auth.common.inputs.email.label")}
|
|
|
|
|
placeholder={t("auth.common.inputs.email.placeholder")}
|
|
|
|
|
autoComplete="email"
|
|
|
|
|
value={form.email}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
error={errors.email ? true : false}
|
|
|
|
|
helperText={errors.email ? t(errors.email) : ""} // Localization keys are in validation.js
|
2025-06-11 10:20:14 +08:00
|
|
|
/>
|
2025-08-06 17:17:44 -04:00
|
|
|
<TextInput
|
|
|
|
|
type="password"
|
|
|
|
|
name="password"
|
|
|
|
|
label={t("auth.common.inputs.password.label")}
|
|
|
|
|
placeholder="••••••••••"
|
|
|
|
|
autoComplete="current-password"
|
|
|
|
|
value={form.password}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
error={errors.password ? true : false}
|
|
|
|
|
helperText={errors.password ? t(errors.password) : ""} // Localization keys are in validation.js
|
|
|
|
|
endAdornment={<PasswordEndAdornment />}
|
2025-06-11 10:20:14 +08:00
|
|
|
/>
|
2025-08-06 17:17:44 -04:00
|
|
|
<Button
|
|
|
|
|
variant="contained"
|
|
|
|
|
color="accent"
|
|
|
|
|
type="submit"
|
|
|
|
|
sx={{ width: "100%", alignSelf: "center", fontWeight: 700 }}
|
|
|
|
|
>
|
|
|
|
|
Login
|
|
|
|
|
</Button>
|
2025-06-11 10:20:14 +08:00
|
|
|
</Stack>
|
2025-08-06 17:17:44 -04:00
|
|
|
<TextLink
|
|
|
|
|
text={t("auth.login.links.forgotPassword")}
|
|
|
|
|
linkText={t("auth.login.links.forgotPasswordLink")}
|
|
|
|
|
href="/forgot-password"
|
|
|
|
|
/>
|
|
|
|
|
<TextLink
|
|
|
|
|
text={t("auth.login.links.register")}
|
|
|
|
|
linkText={t("auth.login.links.registerLink")}
|
|
|
|
|
href="/register"
|
|
|
|
|
/>
|
|
|
|
|
</AuthPageWrapper>
|
2025-06-11 10:20:14 +08:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default Login;
|