diff --git a/client/src/Pages/Auth/Login/index.tsx b/client/src/Pages/Auth/Login/index.tsx index ec95a0ce3..9de5f1df5 100644 --- a/client/src/Pages/Auth/Login/index.tsx +++ b/client/src/Pages/Auth/Login/index.tsx @@ -1,6 +1,7 @@ import { BaseAuthPage, TextLink } from "@/Components/design-elements"; import { Button, TextField } from "@/Components/inputs"; +import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useForm, Controller } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js"; @@ -9,7 +10,7 @@ import type { LoginFormData } from "@/Validation/login"; import { useDispatch } from "react-redux"; import { useNavigate } from "react-router-dom"; import { setAuthState } from "@/Features/Auth/authSlice"; -import { usePost } from "@/Hooks/UseApi"; +import { useLazyGet, usePost } from "@/Hooks/UseApi"; const LoginPage = () => { const { t } = useTranslation(); @@ -17,6 +18,17 @@ const LoginPage = () => { const navigate = useNavigate(); const { post, loading } = usePost(); + const [isCheckingAdmin, setIsCheckingAdmin] = useState(true); + const { get } = useLazyGet(); + + useEffect(() => { + get("/auth/users/superadmin").then((res) => { + if (res?.data === false) navigate("/register", { replace: true }); + else setIsCheckingAdmin(false); + }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); + const { schema, defaults } = useLoginForm(); const { control, handleSubmit } = useForm({ @@ -24,6 +36,8 @@ const LoginPage = () => { defaultValues: defaults, }); + if (isCheckingAdmin) return null; + const onSubmit = async (data: LoginFormData) => { if (loading) return; @@ -82,12 +96,6 @@ const LoginPage = () => { linkText={t("pages.auth.login.links.forgotPassword.linkText")} href="/forgot-password" /> - ); }; diff --git a/client/src/Pages/Auth/Register/index.tsx b/client/src/Pages/Auth/Register/index.tsx index a2a439023..71603058f 100644 --- a/client/src/Pages/Auth/Register/index.tsx +++ b/client/src/Pages/Auth/Register/index.tsx @@ -1,16 +1,17 @@ import { BaseAuthPage } from "@/Components/design-elements"; import { Button, TextField } from "@/Components/inputs"; +import Alert from "@mui/material/Alert"; import { useForm, Controller } from "react-hook-form"; import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js"; import { useRegisterForm } from "@/Hooks/useRegisterForm"; import type { RegisterFormData } from "@/Validation/register"; import { useTranslation } from "react-i18next"; -import { usePost, useGet } from "@/Hooks/UseApi"; +import { useLazyGet, usePost } from "@/Hooks/UseApi"; import { setAuthState } from "@/Features/Auth/authSlice"; import { useDispatch } from "react-redux"; import { useNavigate, useParams } from "react-router-dom"; import type { AuthResponse } from "@/Types/User"; -import { useEffect, useRef } from "react"; +import { useEffect, useRef, useState } from "react"; interface RegisterPayload { user: Omit; @@ -32,21 +33,23 @@ const RegisterPage = () => { const { post: verifyToken } = usePost<{ token: string }, InviteVerifyResponse>(); const hasVerified = useRef(false); - const { data: superAdminExists, isLoading: isCheckingAdmin } = useGet( - token ? null : "/auth/users/superadmin" - ); + const [isCheckingAdmin, setIsCheckingAdmin] = useState(!token); + const { get } = useLazyGet(); + + useEffect(() => { + if (token) return; + get("/auth/users/superadmin").then((res) => { + if (res?.data === true) navigate("/login", { replace: true }); + else setIsCheckingAdmin(false); + }); + // eslint-disable-next-line react-hooks/exhaustive-deps + }, []); const { control, handleSubmit, setError, reset } = useForm({ resolver: zodResolver(schema), defaultValues: defaults, }); - useEffect(() => { - if (superAdminExists === true) { - navigate("/login", { replace: true }); - } - }, [superAdminExists, navigate]); - useEffect(() => { if (!token || hasVerified.current) return; hasVerified.current = true; @@ -92,6 +95,7 @@ const RegisterPage = () => { title={t("pages.auth.register.title")} subtitle={t("pages.auth.register.subtitle")} > + {!token && {t("pages.auth.register.setupNotice")}}