diff --git a/Client/src/HOC/withAdminCheck.jsx b/Client/src/HOC/withAdminCheck.jsx new file mode 100644 index 000000000..2d8812728 --- /dev/null +++ b/Client/src/HOC/withAdminCheck.jsx @@ -0,0 +1,31 @@ +import React, { useEffect } from "react"; +import { useNavigate } from "react-router-dom"; +import axiosInstance from "../Utils/axiosConfig"; + +const withAdminCheck = (WrappedComponent) => { + const WithAdminCheck = (props) => { + const navigate = useNavigate(); + + useEffect(() => { + console.log("admin check"); + axiosInstance + .get("/auth/users/admin") + .then((response) => { + if (response.data.data === true) { + navigate("/login"); + } + }) + .catch((error) => { + console.log(error); + }); + }, [navigate]); + return ; + }; + const wrappedComponentName = + WrappedComponent.displayName || WrappedComponent.name || "Component"; + WithAdminCheck.displayName = `WithAdminCheck(${wrappedComponentName})`; + + return WithAdminCheck; +}; + +export default withAdminCheck; diff --git a/Client/src/Pages/Auth/Register/Register.jsx b/Client/src/Pages/Auth/Register/Register.jsx index 3003a3a75..dae616d3a 100644 --- a/Client/src/Pages/Auth/Register/Register.jsx +++ b/Client/src/Pages/Auth/Register/Register.jsx @@ -1,4 +1,4 @@ -import { useState, useEffect } from "react"; +import { useState } from "react"; import { useNavigate } from "react-router-dom"; import { useTheme } from "@emotion/react"; import { Stack, Typography } from "@mui/material"; @@ -10,10 +10,10 @@ import Logomark from "../../../assets/Images/bwl-logo-2.svg?react"; import Check from "../../../Components/Check/Check"; import Button from "../../../Components/Button"; import { credentials } from "../../../Validation/validation"; -import axiosInstance from "../../../Utils/axiosConfig"; -import { register } from "../../../Features/Auth/authSlice"; import { createToast } from "../../../Utils/toastUtils"; import Field from "../../../Components/Inputs/Field"; +import withAdminCheck from "../../../HOC/withAdminCheck"; +import { register } from "../../../Features/Auth/authSlice"; const Register = () => { const dispatch = useDispatch(); @@ -39,19 +39,6 @@ const Register = () => { }); const [errors, setErrors] = useState({}); - useEffect(() => { - axiosInstance - .get("/auth/users/admin") - .then((response) => { - if (response.data.data === true) { - navigate("/login"); - } - }) - .catch((error) => { - console.log(error); - }); - }, [form, navigate]); - const handleSubmit = async (e) => { e.preventDefault(); @@ -242,4 +229,5 @@ const Register = () => { ); }; -export default Register; +const WrappedRegister = withAdminCheck(Register); +export default WrappedRegister;