move admin check to regsiter page
This commit is contained in:
1 parent
ee3062fcb4
commit
de447d7be2
5 files changed
+61
-52
No files matched your search
@@ -1,49 +0,0 @@
|
|||||||
import { useEffect, useState } from "react";
|
|
||||||
import { useNavigate } from "react-router-dom";
|
|
||||||
|
|
||||||
import { logger } from "../../../Utils/Logger.js";
|
|
||||||
import { useLazyGet } from "@/Hooks/UseApi";
|
|
||||||
|
|
||||||
const withAdminCheck = (WrappedComponent) => {
|
|
||||||
const WithAdminCheck = (props) => {
|
|
||||||
const navigate = useNavigate();
|
|
||||||
const [superAdminExists, setSuperAdminExists] = useState(false);
|
|
||||||
const [hasChecked, setHasChecked] = useState(false);
|
|
||||||
const { get: checkSuperAdmin, loading: isChecking } = useLazyGet();
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
checkSuperAdmin("/auth/users/superadmin")
|
|
||||||
.then((response) => {
|
|
||||||
if (response?.data === true) {
|
|
||||||
navigate("/login");
|
|
||||||
} else {
|
|
||||||
setSuperAdminExists(false);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
logger.error(error);
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
setHasChecked(true);
|
|
||||||
});
|
|
||||||
}, [navigate, checkSuperAdmin]);
|
|
||||||
|
|
||||||
if (!hasChecked || isChecking) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<WrappedComponent
|
|
||||||
{...props}
|
|
||||||
superAdminExists={superAdminExists}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
const wrappedComponentName =
|
|
||||||
WrappedComponent.displayName || WrappedComponent.name || "Component";
|
|
||||||
WithAdminCheck.displayName = `WithAdminCheck(${wrappedComponentName})`;
|
|
||||||
|
|
||||||
return WithAdminCheck;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default withAdminCheck;
|
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
import { Navigate } from "react-router-dom";
|
||||||
|
import { useSelector } from "react-redux";
|
||||||
|
import type { RootState } from "@/Types/state";
|
||||||
|
import type { UserRole } from "@/Types/User";
|
||||||
|
|
||||||
|
interface ProtectedRouteProps {
|
||||||
|
children: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
|
||||||
|
const authState = useSelector((state: RootState) => state.auth);
|
||||||
|
|
||||||
|
return authState.authToken ? (
|
||||||
|
children
|
||||||
|
) : (
|
||||||
|
<Navigate
|
||||||
|
to="/login"
|
||||||
|
replace
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
interface RoleProtectedRouteProps {
|
||||||
|
roles: UserRole[];
|
||||||
|
children: React.ReactNode;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const RoleProtectedRoute = ({ roles, children }: RoleProtectedRouteProps) => {
|
||||||
|
const authState = useSelector((state: RootState) => state.auth);
|
||||||
|
const userRoles = authState?.user?.role || [];
|
||||||
|
const canAccess = userRoles.some((role) => roles.includes(role));
|
||||||
|
|
||||||
|
return canAccess ? (
|
||||||
|
children
|
||||||
|
) : (
|
||||||
|
<Navigate
|
||||||
|
to="/uptime"
|
||||||
|
replace
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
Whitespace-only changes.
@@ -5,7 +5,7 @@ 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 } from "@/Hooks/UseApi";
|
import { usePost, useGet } 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";
|
||||||
@@ -32,11 +32,21 @@ 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>(
|
||||||
|
token ? null : "/auth/users/superadmin"
|
||||||
|
);
|
||||||
|
|
||||||
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;
|
||||||
@@ -53,6 +63,8 @@ const RegisterPage = () => {
|
|||||||
});
|
});
|
||||||
}, [token]);
|
}, [token]);
|
||||||
|
|
||||||
|
if (isCheckingAdmin) return null;
|
||||||
|
|
||||||
const onSubmit = async (data: RegisterFormData) => {
|
const onSubmit = async (data: RegisterFormData) => {
|
||||||
if (loading) return;
|
if (loading) return;
|
||||||
|
|
||||||
|
|||||||
@@ -47,10 +47,15 @@ import Settings from "@/Pages/Settings";
|
|||||||
import Maintenance from "@/Pages/Maintenance";
|
import Maintenance from "@/Pages/Maintenance";
|
||||||
import CreateNewMaintenanceWindow from "@/Pages/Maintenance/create";
|
import CreateNewMaintenanceWindow from "@/Pages/Maintenance/create";
|
||||||
|
|
||||||
import ProtectedRoute from "@/Components/v1/ProtectedRoute";
|
// Logs & Diagnostics
|
||||||
import RoleProtectedRoute from "@/Components/v1/RoleProtectedRoute";
|
|
||||||
import Logs from "@/Pages/Logs";
|
import Logs from "@/Pages/Logs";
|
||||||
|
|
||||||
|
// Routing
|
||||||
|
import {
|
||||||
|
ProtectedRoute,
|
||||||
|
RoleProtectedRoute,
|
||||||
|
} from "@/Components/v2/routing/RouteProtected";
|
||||||
|
|
||||||
import CreateMonitor from "@/Pages/CreateMonitor";
|
import CreateMonitor from "@/Pages/CreateMonitor";
|
||||||
|
|
||||||
const Routes = () => {
|
const Routes = () => {
|
||||||
|
|||||||
Reference in new issue
Block a user