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 { BaseAuthPage, TextLink } from "@/Components/design-elements";
|
||||||
import { Button, TextField } from "@/Components/inputs";
|
import { Button, TextField } from "@/Components/inputs";
|
||||||
|
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { useForm, Controller } from "react-hook-form";
|
import { useForm, Controller } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
|
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 { useDispatch } from "react-redux";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { setAuthState } from "@/Features/Auth/authSlice";
|
import { setAuthState } from "@/Features/Auth/authSlice";
|
||||||
import { usePost } from "@/Hooks/UseApi";
|
import { useLazyGet, usePost } from "@/Hooks/UseApi";
|
||||||
|
|
||||||
const LoginPage = () => {
|
const LoginPage = () => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
@@ -17,6 +18,17 @@ const LoginPage = () => {
|
|||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { post, loading } = usePost();
|
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 { schema, defaults } = useLoginForm();
|
||||||
|
|
||||||
const { control, handleSubmit } = useForm<LoginFormData>({
|
const { control, handleSubmit } = useForm<LoginFormData>({
|
||||||
@@ -24,6 +36,8 @@ const LoginPage = () => {
|
|||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (isCheckingAdmin) return null;
|
||||||
|
|
||||||
const onSubmit = async (data: LoginFormData) => {
|
const onSubmit = async (data: LoginFormData) => {
|
||||||
if (loading) return;
|
if (loading) return;
|
||||||
|
|
||||||
@@ -82,12 +96,6 @@ const LoginPage = () => {
|
|||||||
linkText={t("pages.auth.login.links.forgotPassword.linkText")}
|
linkText={t("pages.auth.login.links.forgotPassword.linkText")}
|
||||||
href="/forgot-password"
|
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>
|
</BaseAuthPage>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
import { BaseAuthPage } from "@/Components/design-elements";
|
import { BaseAuthPage } from "@/Components/design-elements";
|
||||||
import { Button, TextField } from "@/Components/inputs";
|
import { Button, TextField } from "@/Components/inputs";
|
||||||
|
import Alert from "@mui/material/Alert";
|
||||||
import { useForm, Controller } from "react-hook-form";
|
import { useForm, Controller } from "react-hook-form";
|
||||||
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
|
import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
|
||||||
import { useRegisterForm } from "@/Hooks/useRegisterForm";
|
import { useRegisterForm } from "@/Hooks/useRegisterForm";
|
||||||
import type { RegisterFormData } from "@/Validation/register";
|
import type { RegisterFormData } from "@/Validation/register";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import { usePost, useGet } from "@/Hooks/UseApi";
|
import { useLazyGet, usePost } from "@/Hooks/UseApi";
|
||||||
import { setAuthState } from "@/Features/Auth/authSlice";
|
import { setAuthState } from "@/Features/Auth/authSlice";
|
||||||
import { useDispatch } from "react-redux";
|
import { useDispatch } from "react-redux";
|
||||||
import { useNavigate, useParams } from "react-router-dom";
|
import { useNavigate, useParams } from "react-router-dom";
|
||||||
import type { AuthResponse } from "@/Types/User";
|
import type { AuthResponse } from "@/Types/User";
|
||||||
import { useEffect, useRef } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
interface RegisterPayload {
|
interface RegisterPayload {
|
||||||
user: Omit<RegisterFormData, "confirm">;
|
user: Omit<RegisterFormData, "confirm">;
|
||||||
@@ -32,21 +33,23 @@ const RegisterPage = () => {
|
|||||||
const { post: verifyToken } = usePost<{ token: string }, InviteVerifyResponse>();
|
const { post: verifyToken } = usePost<{ token: string }, InviteVerifyResponse>();
|
||||||
const hasVerified = useRef(false);
|
const hasVerified = useRef(false);
|
||||||
|
|
||||||
const { data: superAdminExists, isLoading: isCheckingAdmin } = useGet<boolean>(
|
const [isCheckingAdmin, setIsCheckingAdmin] = useState(!token);
|
||||||
token ? null : "/auth/users/superadmin"
|
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>({
|
const { control, handleSubmit, setError, reset } = useForm<RegisterFormData>({
|
||||||
resolver: zodResolver(schema),
|
resolver: zodResolver(schema),
|
||||||
defaultValues: defaults,
|
defaultValues: defaults,
|
||||||
});
|
});
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (superAdminExists === true) {
|
|
||||||
navigate("/login", { replace: true });
|
|
||||||
}
|
|
||||||
}, [superAdminExists, navigate]);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!token || hasVerified.current) return;
|
if (!token || hasVerified.current) return;
|
||||||
hasVerified.current = true;
|
hasVerified.current = true;
|
||||||
@@ -92,6 +95,7 @@ const RegisterPage = () => {
|
|||||||
title={t("pages.auth.register.title")}
|
title={t("pages.auth.register.title")}
|
||||||
subtitle={t("pages.auth.register.subtitle")}
|
subtitle={t("pages.auth.register.subtitle")}
|
||||||
>
|
>
|
||||||
|
{!token && <Alert severity="info">{t("pages.auth.register.setupNotice")}</Alert>}
|
||||||
<Controller
|
<Controller
|
||||||
name="firstName"
|
name="firstName"
|
||||||
control={control}
|
control={control}
|
||||||
|
|||||||
@@ -343,6 +343,7 @@
|
|||||||
"register": {
|
"register": {
|
||||||
"title": "Welcome back to Checkmate!",
|
"title": "Welcome back to Checkmate!",
|
||||||
"subtitle": "Sign up to get started",
|
"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"
|
"submit": "Register"
|
||||||
},
|
},
|
||||||
"forgotPassword": {
|
"forgotPassword": {
|
||||||
|
|||||||
Reference in new issue
Block a user