Files
Checkmate/client/src/Pages/Auth/Login/index.tsx
T

97 lines
2.7 KiB
TypeScript
Raw Normal View History

2026-02-20 18:13:39 +00:00
import { BaseAuthPage, TextLink } from "@/Components/design-elements";
import { Button, TextField } from "@/Components/inputs";
import { useTranslation } from "react-i18next";
2026-02-06 20:53:48 +00:00
import { useForm, Controller } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
import { useLoginForm } from "@/Hooks/useLoginForm";
import type { LoginFormData } from "@/Validation/login";
2026-02-06 21:11:46 +00:00
import { useDispatch } from "react-redux";
import { useNavigate } from "react-router-dom";
import { setAuthState } from "@/Features/Auth/authSlice";
import { usePost } from "@/Hooks/UseApi";
import { useSuperAdminRedirect } from "@/Hooks/useSuperAdminRedirect";
2026-02-06 20:53:48 +00:00
const LoginPage = () => {
const { t } = useTranslation();
2026-02-06 21:11:46 +00:00
const dispatch = useDispatch();
const navigate = useNavigate();
const { post, loading } = usePost();
2026-02-06 20:53:48 +00:00
const { isLoading: isCheckingAdmin } = useSuperAdminRedirect({
redirectTo: "/register",
redirectWhen: false,
});
2026-02-06 20:53:48 +00:00
const { schema, defaults } = useLoginForm();
2026-02-06 21:35:25 +00:00
const { control, handleSubmit } = useForm<LoginFormData>({
2026-02-06 20:53:48 +00:00
resolver: zodResolver(schema),
defaultValues: defaults,
});
if (isCheckingAdmin) return null;
2026-02-06 21:11:46 +00:00
const onSubmit = async (data: LoginFormData) => {
if (loading) return;
const result = await post("/auth/login", data);
if (result?.success) {
2026-02-06 21:35:25 +00:00
dispatch(setAuthState(result));
navigate("/uptime");
2026-02-06 21:11:46 +00:00
}
2026-02-06 20:53:48 +00:00
};
return (
<BaseAuthPage
2026-02-06 20:53:48 +00:00
component="form"
onSubmit={handleSubmit(onSubmit)}
title={t("pages.auth.login.title")}
subtitle={t("pages.auth.login.subtitle")}
2026-02-06 20:53:48 +00:00
>
<Controller
name="email"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
fieldLabel={t("pages.auth.common.form.option.email.label")}
placeholder={t("pages.auth.common.form.option.email.placeholder")}
error={!!fieldState.error}
helperText={fieldState.error ? t(fieldState.error.message ?? "") : ""}
/>
)}
/>
<Controller
name="password"
control={control}
render={({ field, fieldState }) => (
<TextField
{...field}
type="password"
fieldLabel={t("pages.auth.common.form.option.password.label")}
placeholder={t("pages.auth.common.form.option.password.placeholder")}
error={!!fieldState.error}
helperText={fieldState.error ? t(fieldState.error.message ?? "") : ""}
/>
)}
/>
<Button
variant="contained"
type="submit"
2026-02-06 21:11:46 +00:00
loading={loading}
2026-02-06 20:53:48 +00:00
>
{t("pages.auth.login.submit")}
</Button>
2026-02-06 21:50:45 +00:00
<TextLink
alignSelf={"center"}
text={t("pages.auth.login.links.forgotPassword.text")}
linkText={t("pages.auth.login.links.forgotPassword.linkText")}
href="/forgot-password"
/>
2026-02-06 20:53:48 +00:00
</BaseAuthPage>
);
};
export default LoginPage;