Skip to content

Commit de447d7

Browse files
committed
move admin check to regsiter page
1 parent ee3062f commit de447d7

File tree

5 files changed

+61
-52
lines changed

5 files changed

+61
-52
lines changed

client/src/Components/v1/HOC/withAdminCheck.jsx

Lines changed: 0 additions & 49 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Navigate } from "react-router-dom";
2+
import { useSelector } from "react-redux";
3+
import type { RootState } from "@/Types/state";
4+
import type { UserRole } from "@/Types/User";
5+
6+
interface ProtectedRouteProps {
7+
children: React.ReactNode;
8+
}
9+
10+
export const ProtectedRoute = ({ children }: ProtectedRouteProps) => {
11+
const authState = useSelector((state: RootState) => state.auth);
12+
13+
return authState.authToken ? (
14+
children
15+
) : (
16+
<Navigate
17+
to="/login"
18+
replace
19+
/>
20+
);
21+
};
22+
23+
interface RoleProtectedRouteProps {
24+
roles: UserRole[];
25+
children: React.ReactNode;
26+
}
27+
28+
export const RoleProtectedRoute = ({ roles, children }: RoleProtectedRouteProps) => {
29+
const authState = useSelector((state: RootState) => state.auth);
30+
const userRoles = authState?.user?.role || [];
31+
const canAccess = userRoles.some((role) => roles.includes(role));
32+
33+
return canAccess ? (
34+
children
35+
) : (
36+
<Navigate
37+
to="/uptime"
38+
replace
39+
/>
40+
);
41+
};

client/src/Components/v2/routing/RouteRoleProtected.tsx

Whitespace-only changes.

client/src/Pages/Auth/Register/index.tsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { zodResolver } from "@hookform/resolvers/zod/dist/zod.js";
55
import { useRegisterForm } from "@/Hooks/useRegisterForm";
66
import type { RegisterFormData } from "@/Validation/register";
77
import { useTranslation } from "react-i18next";
8-
import { usePost } from "@/Hooks/UseApi";
8+
import { usePost, useGet } from "@/Hooks/UseApi";
99
import { setAuthState } from "@/Features/Auth/authSlice";
1010
import { useDispatch } from "react-redux";
1111
import { useNavigate, useParams } from "react-router-dom";
@@ -32,11 +32,21 @@ const RegisterPage = () => {
3232
const { post: verifyToken } = usePost<{ token: string }, InviteVerifyResponse>();
3333
const hasVerified = useRef(false);
3434

35+
const { data: superAdminExists, isLoading: isCheckingAdmin } = useGet<boolean>(
36+
token ? null : "/auth/users/superadmin"
37+
);
38+
3539
const { control, handleSubmit, setError, reset } = useForm<RegisterFormData>({
3640
resolver: zodResolver(schema),
3741
defaultValues: defaults,
3842
});
3943

44+
useEffect(() => {
45+
if (superAdminExists === true) {
46+
navigate("/login", { replace: true });
47+
}
48+
}, [superAdminExists, navigate]);
49+
4050
useEffect(() => {
4151
if (!token || hasVerified.current) return;
4252
hasVerified.current = true;
@@ -53,6 +63,8 @@ const RegisterPage = () => {
5363
});
5464
}, [token]);
5565

66+
if (isCheckingAdmin) return null;
67+
5668
const onSubmit = async (data: RegisterFormData) => {
5769
if (loading) return;
5870

client/src/Routes/index.jsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,15 @@ import Settings from "@/Pages/Settings";
4747
import Maintenance from "@/Pages/Maintenance";
4848
import CreateNewMaintenanceWindow from "@/Pages/Maintenance/create";
4949

50-
import ProtectedRoute from "@/Components/v1/ProtectedRoute";
51-
import RoleProtectedRoute from "@/Components/v1/RoleProtectedRoute";
50+
// Logs & Diagnostics
5251
import Logs from "@/Pages/Logs";
5352

53+
// Routing
54+
import {
55+
ProtectedRoute,
56+
RoleProtectedRoute,
57+
} from "@/Components/v2/routing/RouteProtected";
58+
5459
import CreateMonitor from "@/Pages/CreateMonitor";
5560

5661
const Routes = () => {

0 commit comments

Comments
 (0)