fix: hide register tip on login and redirect to setup when no super admin (#3556)
## Changes - Removed the "Register here" link from the login page — it misled users into thinking public registration was open. - Added redirect from /login to /register when no super admin exists (initial setup). - Added an info alert on the register page clarifying the page is only for initial setup; additional users are invited from team settings. Hidden for the invite-token flow. - Extracted shared useSuperAdminRedirect hook to dedupe identical fetch/redirect logic across Login and Register. ## Benefits - Clearer UX: login page no longer suggests self-registration is available post-setup. - Correct routing on fresh installs: users land on the registration page instead of a login page they can't use.
This commit is contained in:
1 parent
247308b636
commit
4998fe4c94
4 files changed
+44
-16
No files matched your search
@@ -0,0 +1,24 @@
|
||||
import { useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useGet } from "@/Hooks/UseApi";
|
||||
|
||||
type Options = {
|
||||
redirectTo: string;
|
||||
redirectWhen: boolean;
|
||||
skip?: boolean;
|
||||
};
|
||||
|
||||
export const useSuperAdminRedirect = ({ redirectTo, redirectWhen, skip }: Options) => {
|
||||
const navigate = useNavigate();
|
||||
const { data: superAdminExists, isLoading } = useGet<boolean>(
|
||||
skip ? null : "/auth/users/superadmin"
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (!skip && superAdminExists === redirectWhen) {
|
||||
navigate(redirectTo, { replace: true });
|
||||
}
|
||||
}, [superAdminExists, redirectWhen, redirectTo, skip, navigate]);
|
||||
|
||||
return { superAdminExists, isLoading };
|
||||
};
|
||||
@@ -10,6 +10,7 @@ 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";
|
||||
|
||||
const LoginPage = () => {
|
||||
const { t } = useTranslation();
|
||||
@@ -17,6 +18,11 @@ const LoginPage = () => {
|
||||
const navigate = useNavigate();
|
||||
const { post, loading } = usePost();
|
||||
|
||||
const { isLoading: isCheckingAdmin } = useSuperAdminRedirect({
|
||||
redirectTo: "/register",
|
||||
redirectWhen: false,
|
||||
});
|
||||
|
||||
const { schema, defaults } = useLoginForm();
|
||||
|
||||
const { control, handleSubmit } = useForm<LoginFormData>({
|
||||
@@ -24,6 +30,8 @@ const LoginPage = () => {
|
||||
defaultValues: defaults,
|
||||
});
|
||||
|
||||
if (isCheckingAdmin) return null;
|
||||
|
||||
const onSubmit = async (data: LoginFormData) => {
|
||||
if (loading) return;
|
||||
|
||||
@@ -82,12 +90,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,11 +1,13 @@
|
||||
import { BaseAuthPage } from "@/Components/design-elements";
|
||||
import { Button, TextField } from "@/Components/inputs";
|
||||
import { Alert } from "@mui/material";
|
||||
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 { usePost } from "@/Hooks/UseApi";
|
||||
import { useSuperAdminRedirect } from "@/Hooks/useSuperAdminRedirect";
|
||||
import { setAuthState } from "@/Features/Auth/authSlice";
|
||||
import { useDispatch } from "react-redux";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
@@ -32,21 +34,17 @@ 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 { isLoading: isCheckingAdmin } = useSuperAdminRedirect({
|
||||
redirectTo: "/login",
|
||||
redirectWhen: true,
|
||||
skip: !!token,
|
||||
});
|
||||
|
||||
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 +90,9 @@ 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