Merge pull request #3557 from bluewave-labs/fix/hide-register-tip-on-login
fix: hide register tip on login and redirect to setup when no super admin
This commit is contained in:
3 files changed
+31
-18
No files matched your search
@@ -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<boolean>();
|
||||
|
||||
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<LoginFormData>({
|
||||
@@ -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"
|
||||
/>
|
||||
<TextLink
|
||||
alignSelf={"center"}
|
||||
text={t("pages.auth.login.links.register.text")}
|
||||
linkText={t("pages.auth.login.links.register.linkText")}
|
||||
href="/register"
|
||||
/>
|
||||
</BaseAuthPage>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -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<RegisterFormData, "confirm">;
|
||||
@@ -32,21 +33,23 @@ const RegisterPage = () => {
|
||||
const { post: verifyToken } = usePost<{ token: string }, InviteVerifyResponse>();
|
||||
const hasVerified = useRef(false);
|
||||
|
||||
const { data: superAdminExists, isLoading: isCheckingAdmin } = useGet<boolean>(
|
||||
token ? null : "/auth/users/superadmin"
|
||||
);
|
||||
const [isCheckingAdmin, setIsCheckingAdmin] = useState(!token);
|
||||
const { get } = useLazyGet<boolean>();
|
||||
|
||||
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<RegisterFormData>({
|
||||
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 && <Alert severity="info">{t("pages.auth.register.setupNotice")}</Alert>}
|
||||
<Controller
|
||||
name="firstName"
|
||||
control={control}
|
||||
|
||||
@@ -343,6 +343,7 @@
|
||||
"register": {
|
||||
"title": "Welcome back to Checkmate!",
|
||||
"subtitle": "Sign up to get started",
|
||||
"setupNotice": "This page is only available during initial setup. Once a super admin is registered, additional users can be invited from team settings.",
|
||||
"submit": "Register"
|
||||
},
|
||||
"forgotPassword": {
|
||||
|
||||
Reference in new issue
Block a user